You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/desugarEnums.md
+63-44Lines changed: 63 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -41,106 +41,125 @@ comma separated simple cases into a sequence of cases.
41
41
42
42
2. An enum class definition
43
43
44
-
enum class E ... extends <parents> ...
44
+
enum class E ... extends <parents> ...
45
45
46
46
expands to a `sealed``abstract` class that extends the `scala.Enum` trait:
47
47
48
48
sealed abstract class E ... extends <parents> with scala.Enum ...
49
49
50
50
3. If `E` is an enum class without type parameters, then a case in its companion object without an extends clause
51
51
52
-
case C <params> <body>
52
+
case C <params> <body>
53
53
54
54
expands to
55
55
56
-
case C <params> <body> extends E
56
+
case C <params> <body> extends E
57
57
58
-
4. If `E` is an enum class with type parameters `Ts`, then a case in its companion object without an extends clause
58
+
4. If `E` is an enum class with type parameters `Ts`, then a case in its
59
+
companion object without an extends clause
59
60
60
-
case C <params> <body>
61
+
case C <params> <body>
61
62
62
-
expands according to two alternatives, depending whether `C` has type parameters or not. If `C` has type parameters, they must have the same names and appear in the same order as the enum type parameters `Ts` (variances may be different, however). In this case
63
+
expands according to two alternatives, depending whether `C` has type
64
+
parameters or not. If `C` has type parameters, they must have the same
65
+
names and appear in the same order as the enum type parameters `Ts`
66
+
(variances may be different, however). In this case
63
67
64
-
case C [Ts] <params> <body>
68
+
case C [Ts] <params> <body>
65
69
66
-
expands to
70
+
expands to
67
71
68
-
case C[Ts] <params> extends E[Ts] <body>
72
+
case C[Ts] <params> extends E[Ts] <body>
69
73
70
-
For the case where `C` does not have type parameters, assume `E`'s type parameters are
74
+
For the case where `C` does not have type parameters, assume `E`'s type
where each of the variances `Vi` is either `'+'` or `'-'`. Then the case expands to
79
+
where each of the variances `Vi` is either `'+'` or `'-'`. Then the case
80
+
expands to
75
81
76
-
case C <params> extends E[B1, ..., Bn] <body>
82
+
case C <params> extends E[B1, ..., Bn] <body>
77
83
78
-
where `Bi` is `Li` if `Vi = '+'` and `Ui` if `Vi = '-'`. It is an error if `Bi` refers to some other type parameter `Tj (j = 0,..,n-1)`. It is also an error if `E` has type parameters that are non-variant.
84
+
where `Bi` is `Li` if `Vi = '+'` and `Ui` if `Vi = '-'`. It is an error if
85
+
`Bi` refers to some other type parameter `Tj (j = 0,..,n-1)`. It is also
86
+
an error if `E` has type parameters that are non-variant.
79
87
80
88
5. A class case
81
89
82
-
case C <params> ...
90
+
case C <params> ...
83
91
84
-
expands analogous to a case class:
92
+
expands analogous to a case class:
85
93
86
-
final case class C <params> ...
94
+
final case class C <params> ...
87
95
88
-
However, unlike for a regular case class, the return type of the associated `apply` method is a fully parameterized type instance of the enum class `E` itself instead of `C`. Also the enum case defines an `enumTag` method of the form
96
+
However, unlike for a regular case class, the return type of the associated
97
+
`apply` method is a fully parameterized type instance of the enum class `E`
98
+
itself instead of `C`. Also the enum case defines an `enumTag` method of
99
+
the form
89
100
90
-
def enumTag = n
101
+
def enumTag = n
91
102
92
-
where `n` is the ordinal number of the case in the companion object, starting from 0.
103
+
where `n` is the ordinal number of the case in the companion object,
104
+
starting from 0.
93
105
94
106
6. A value case
95
107
96
-
case C extends <parents> <body>
108
+
case C extends <parents> <body>
97
109
98
-
expands to a value definition
110
+
expands to a value definition
99
111
100
-
val C = new <parents> { <body>; def enumTag = n; $values.register(this) }
112
+
val C = new <parents> { <body>; def enumTag = n; $values.register(this) }
101
113
102
-
where `n` is the ordinal number of the case in the companion object, starting from 0.
103
-
The statement `$values.register(this)` registers the value as one of the `enumValues` of the
104
-
enumeration (see below). `$values` is a compiler-defined private value in
105
-
the companion object.
114
+
where `n` is the ordinal number of the case in the companion object,
115
+
starting from 0. The statement `$values.register(this)` registers the value
116
+
as one of the `enumValues` of the enumeration (see below). `$values` is a
117
+
compiler-defined private value in the companion object.
106
118
107
119
7. A simple case
108
120
109
-
case C
121
+
case C
110
122
111
-
of an enum class `E` that does not take type parameters expands to
123
+
of an enum class `E` that does not take type parameters expands to
112
124
113
-
val C = $new(n, "C")
125
+
val C = $new(n, "C")
114
126
115
-
Here, `$new` is a private method that creates an instance of of `E` (see below).
127
+
Here, `$new` is a private method that creates an instance of of `E` (see
128
+
below).
116
129
117
130
8. A simple case consisting of a comma-separated list of enum names
118
131
119
132
case C_1, ..., C_n
120
133
121
-
expands to
134
+
expands to
122
135
123
-
case C_1; ...; case C_n
136
+
case C_1; ...; case C_n
124
137
125
-
Any modifiers or annotations on the original case extend to all expanded cases.
138
+
Any modifiers or annotations on the original case extend to all expanded
139
+
cases.
126
140
127
141
## Translation of Enumerations
128
142
129
143
Non-generic enum classes `E` that define one or more singleton cases
130
144
are called _enumerations_. Companion objects of enumerations define
131
145
the following additional members.
132
146
133
-
- A method `enumValue` of type `scala.collection.immutable.Map[Int, E]`. `enumValue(n)` returns the singleton case value with ordinal number `n`.
134
-
- A method `enumValueNamed` of type `scala.collection.immutable.Map[String, E]`. `enumValueNamed(s)` returns the singleton case value whose `toString` representation is `s`.
135
-
- A method `enumValues` which returns an `Iterable[E]` of all singleton case values in `E`, in the order of their definitions.
147
+
- A method `enumValue` of type `scala.collection.immutable.Map[Int, E]`.
148
+
`enumValue(n)` returns the singleton case value with ordinal number `n`.
149
+
- A method `enumValueNamed` of type `scala.collection.immutable.Map[String, E]`.
150
+
`enumValueNamed(s)` returns the singleton case value whose `toString`
151
+
representation is `s`.
152
+
- A method `enumValues` which returns an `Iterable[E]` of all singleton case
153
+
values in `E`, in the order of their definitions.
136
154
137
155
Companion objects that contain at least one simple case define in addition:
138
156
139
-
- A private method `$new` which defines a new simple case value with given ordinal number and name. This method can be thought as being defined as follows.
140
-
141
-
def $new(tag: Int, name: String): ET = new E {
142
-
def enumTag = tag
143
-
def toString = name
144
-
$values.register(this) // register enum value so that `valueOf` and `values` can return it.
145
-
}
157
+
- A private method `$new` which defines a new simple case value with given
158
+
ordinal number and name. This method can be thought as being defined as
159
+
follows.
146
160
161
+
def $new(tag: Int, name: String): ET = new E {
162
+
def enumTag = tag
163
+
def toString = name
164
+
$values.register(this) // register enum value so that `valueOf` and `values` can return it.
0 commit comments