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
@@ -925,18 +925,15 @@ First, some terminology and notational conventions:
925
925
- We use `<...>` for syntactic constructs that in some circumstances might be empty.
926
926
For instance, `<value-params>` represents one or more parameter lists `(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)` or nothing at all.
927
927
- Enum classes fall into two categories:
928
-
-_parameterized_ enum classes have at least one of the following:
929
-
- a type parameter section, denoted as `[´\mathit{tps}\,´]`;
930
-
- one or more (possibly empty) parameter sections, denoted as `(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)`.
931
-
-_unparameterized_ enum classes have no type parameter sections and no parameter sections.
928
+
-_parameterized_ enum classes have at least one or more (possibly empty) term parameter clauses, denoted as `(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)`.
929
+
-_unparameterized_ enum classes have no term parameter clauses, but may optionally have a type parameter clause, denoted as `[´\mathit{tps}\,´]`.
932
930
- Enum cases fall into three categories:
933
-
934
-
-_Class cases_ are those cases that are parameterized, either with a type parameter section `[´\mathit{tps}\,´]` or with one or more (possibly empty) parameter sections `(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)`.
935
-
-_Simple cases_ are cases of an unparameterized enum that have neither parameters nor an extends clause or body.
931
+
-_Class enum cases_ are those cases that possibly have a type parameter clause `[´\mathit{tps}\,´]`, and necessarily have one or more (possibly empty) parameter clauses `(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)`.
932
+
-_Simple enum cases_ are those cases that have no parameter clauses and no extends clause.
936
933
That is, they consist of a name only.
937
-
-_Value cases_ are all cases that do not have a parameter section but that do have a (possibly generated) `extends` clause and/or a body.
934
+
-_Value enum cases_ are those cases that have no parameter clauses but that do have a (possibly generated) `extends` clause.
938
935
939
-
- Simple cases and value cases are collectively called _singleton cases_.
936
+
- Simple enum cases and value enum cases are collectively called _singleton enum cases_.
940
937
941
938
###### Example
942
939
@@ -970,13 +967,11 @@ enum Option[+T]:
970
967
### Lowering of Enum Definitions
971
968
972
969
###### Summary
973
-
An enum class is represented as a `sealed` class that extends the `scala.reflect.Enum` trait.
970
+
An enum class is represented as a `sealed abstract` class that extends the `scala.reflect.Enum` trait.
974
971
975
972
Enum cases are represented as follows:
976
-
- a class case is mapped to a `case class`,
977
-
- a singleton case is mapped to a `val` definition, where
978
-
- Simple cases all share a single implementation class.
979
-
- Value cases will each be implemented by a unique class.
973
+
- a class enum case is mapped to a `case class` member of enum class' companion object,
974
+
- a singleton enum case is mapped to a `val` member of the enum class' companion object, implemented by a local class definition which may be shared between cases.
980
975
981
976
###### Precise rules
982
977
The `scala.reflect.Enum` trait defines a single public method, `ordinal`:
expands to a `sealed abstract` classthatextends the `scala.reflect.Enum` traitand an associated companion objectthat contains the defined cases, expanded according to rules (2-8).
1001
1000
Theenumclass starts with a compiler-generated importthatimportsthenames`<caseIds>`ofallcasessothattheycanbeusedwithoutprefixintheclass.
1002
1001
```scala
1003
-
sealedabstractclass ´E´ ... extends <parents> with scala.reflect.Enum {
where each of the variances `´\mathit{v}_i´` is either `'+'` or `'-'`, expands to the following value enum case:
1039
+
and where each of the variances `´\mathit{v}_i´` is either `'+'` or `'-'`, expands to the following value enum case:
1040
1040
```scala
1041
1041
case ´C´ extends ´E´[´B_1´, ..., ´B_n´]
1042
1042
```
1043
1043
where `´B_i´` is `´L_i´` if `´\mathit{v}_i´ = '+'` and `´U_i´` if `´\mathit{v}_i´ = '-'`.
1044
-
This result is then further rewritten with rule (8).
1045
-
**NOTE:** It is not permitted for enums with non-variant type parameters to have singleton cases without an extends clause.
1044
+
<p>This result is then further rewritten with rule (8).</p>
1046
1045
1047
-
5. A class case without an extends clause
1046
+
5. A class enum case with type parameters, but without an extends clause
1048
1047
```scala
1049
-
case ´C´ <type-params> <value-params>
1048
+
case ´C´[´\mathit{tps}´](´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)
1050
1049
```
1051
-
of an enum `´E´`that does not take type parameters expands to
1050
+
of an unparameterized enum `´E´`without type parameters expands to
1052
1051
```scala
1053
-
case ´C´ <type-params> <value-params>extends ´E´
1052
+
case ´C´[´\mathit{tps}´](´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)extends ´E´
1054
1053
```
1055
1054
This result is then further rewritten with rule (9).
1056
1055
1057
-
6.If `´E´` is an enum with type parameters `´\mathit{tps}´`, a class case with neither type parameters nor an extends clause
1056
+
6.A class enum case without type parameters or an extends clause
1058
1057
```scala
1059
-
case ´C´ <value-params>
1058
+
case ´C´(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)
1060
1059
```
1061
-
expands to
1060
+
of an unparameterized enum `´E´[´\mathit{tps}´]` with type parameters expands to
1062
1061
```scala
1063
-
case ´C´[´\mathit{tps}´] <value-params>extends ´E´[´\mathit{tps}´]
1062
+
case ´C´(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)extends ´E´[´\mathit{tps}´]
1064
1063
```
1065
-
This result is then further rewritten with rule (9).
1066
-
For class cases that have type parameters themselves, an extends clause needs to be given explicitly.
1067
-
1064
+
This result is then further rewritten with rule (7).
1068
1065
1069
-
7.If `´E´` is an enum with type parameters `´\mathit{tps}´`, a class case without type parameters but with an extends clause
1066
+
7.A class enum case without type parameters, but has an extends clause
1070
1067
```scala
1071
-
case ´C´ <value-params>extends <parents>
1068
+
case ´C´(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)extends <parents>
1072
1069
```
1073
-
expands to
1070
+
of an enum `´E´[´\mathit{tps}´]` with type parameters expands to
1074
1071
```scala
1075
-
case ´C´[´\mathit{tps}´] <value-params>extends <parents>
1072
+
case ´C´[´\mathit{tps}´](´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)extends <parents>
1076
1073
```
1077
-
provided at least one of the parameters `´\mathit{tps}´` is mentioned in a parameter type in `<value-params>` or in a type argument in `<parents>`.
1074
+
provided at least one of the parameters `´\mathit{tps}´` is mentioned in a parameter type in `(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)` or in a type argument in `<parents>`.
1075
+
<br/><br/>
1076
+
This result is then further rewritten with rule (9).
1078
1077
1079
-
8. A value case
1078
+
8. A singleton enum case
1080
1079
```scala
1081
1080
case ´C´ extends <parents>
1082
1081
```
1083
1082
expands to the following `val` definition in `´E´`'s companion object:
1084
1083
```scala
1085
-
val ´C´ =new <parents> { <body>; defordinal= ´\mathit{n}´ }
1084
+
val ´C´ =$factory(_$ordinal = ´\mathit{n}´, $name ="C")
1086
1085
```
1087
1086
where `´\mathit{n}´` is the ordinal number of the case in the companion object, starting from 0.
1087
+
`$factory` is a placeholder that expands its arguments into an expression that produces something equivalent to
1088
+
a new instance of the following (possibly shared) anonymous class:
1089
+
```scala
1090
+
new <parents> {
1091
+
defordinal:Int= _$ordinal
1092
+
overridedeftoString:String= $name
1093
+
}
1094
+
```
1088
1095
The anonymous class also implements the abstract `Product` methods that it inherits from `Enum`.
1096
+
<br/><br/>
1089
1097
**NOTE:** It is an error if a value case refers to a type parameter of `´E´` in a type argument within `<parents>`.
1090
1098
1091
-
9. A class case
1099
+
9. A class enum case
1092
1100
```scala
1093
1101
case ´C´ <type-params> <value-params> extends <parents>
1094
1102
```
@@ -1099,6 +1107,7 @@ Rules (7) to (9) define how such cases with `extends` clauses map into `case cla
1099
1107
}
1100
1108
```
1101
1109
where `´\mathit{n}´` is the ordinal number of the case in the companion object, starting from 0.
1110
+
<br/><br/>
1102
1111
**NOTE:** It is an error if a class case refers to a type parameter of `´E´` in a parameter type in `<type-params>` or `<value-params>` or in a type argument of `<parents>`, unless that parameter is already a type parameter of the case, i.e. the parameter name is defined in `<type-params>`.
Consider the more complex enumeration `Color`, consisting of value enum cases:
1138
-
```scala
1139
-
enumColor(valrgb:Int):
1140
-
caseRedextendsColor(0xFF0000)
1141
-
caseGreenextendsColor(0x00FF00)
1142
-
caseBlueextendsColor(0x0000FF)
1143
-
```
1144
-
1145
-
The three value cases will expand as follows in the companion of `Color`:
1146
-
1147
-
```scala
1148
-
valRed=newColor(0xFF0000):
1149
-
defordinal:Int=0
1150
-
overridedefproductPrefix:String="Red"
1151
-
overridedeftoString:String="Red"
1152
-
valGreen=newColor(0x00FF00):
1153
-
defordinal:Int=1
1154
-
overridedefproductPrefix:String="Green"
1155
-
overridedeftoString:String="Green"
1156
-
valBlue=newColor(0x0000FF):
1157
-
defordinal:Int=2
1158
-
overridedefproductPrefix:String="Blue"
1159
-
overridedeftoString:String="Blue"
1160
-
```
1161
-
1162
1143
### Widening of enum cases post-construction
1163
1144
The compiler-generated `apply` and `copy` methods of an class enum case
1164
1145
```scala
@@ -1176,20 +1157,6 @@ An enum `´E´` (possibly generic) that defines one or more singleton cases, and
1176
1157
It returns the singleton case value whose identifier is `name`.
1177
1158
- A method `values` which returns an `Array[´E'´]` of all singleton case values defined by `E`, in the order of their definitions.
1178
1159
1179
-
### Factory method for simple enum cases
1180
-
1181
-
If an enum `´E´` contains at least one simple case, its companion object will define in addition:
1182
-
1183
-
- A private method `$new` which defines a new simple case value with given ordinal number and name.
1184
-
This method can be thought as being defined as follows.
1185
-
1186
-
```scala
1187
-
privatedef$new(_$ordinal: Int, $name: String): ´E´ with runtime.EnumValue
1188
-
```
1189
-
-`$new` returns a new instance of an anonymous class which implements the abstract `Product` methods that it inherits from `Enum`.
1190
-
- if `´E´` inherits from `java.lang.Enum` the anonymous class does not override the `ordinal` or `toString` methods, as these are final in `java.lang.Enum`.
1191
-
Additionally `productPrefix` will delegate to `this.name`.
1192
-
1193
1160
### Translation of Java-compatible enums
1194
1161
1195
1162
A Java-compatible enum is an enum that extends `java.lang.Enum`.
0 commit comments