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
@@ -41,6 +41,8 @@ The list of parents of a template must be well-formed.
41
41
This means that the class denoted by the superclass constructor ´sc´ must be a subclass of the superclasses of all the traits ´mt_1, ..., mt_n´.
42
42
In other words, the non-trait classes inherited by a template form a chain in the inheritance hierarchy which starts with the template's superclass.
43
43
44
+
It is forbidden for a template's superclass constructor ´sc´ to be an [enum class](#enum-definitions), unless the template is the implementation of an [enum case](#enum-definitions) of ´sc´.
45
+
44
46
The _least proper supertype_ of a template is the class type or [compound type](03-types.html#compound-types) consisting of all its parent class types.
45
47
46
48
The statement sequence ´\mathit{stats}´ contains member definitions that define new members or overwrite members in the parent classes.
EnumCase ::= ‘case’ (id ClassConstr [‘extends’ ConstrApps] | ids)
814
817
```
815
818
816
-
### Enum Cases
817
-
<!-- TODO: Agree with NTs of rest of spec -->
818
-
```ebnf
819
-
EnumCase ::= ‘case’ (id ClassConstr [‘extends’ ConstrApps] | ids)
819
+
An _enum definition_ implies the definition of an _enum class_, a companion object, and one or more _enum cases_.
820
+
821
+
Enum definitions are useful to encode both Generalised Algebraic Data Types and Enumerated Types.
822
+
823
+
The compiler expands enum definitions to code that only uses Scala's other language features.
824
+
As such, enum definitions in Scala are convenient _syntactic sugar_, but they are not essential to understand Scala's core.
825
+
826
+
We now explain the expansion of enum definitions in detail.
827
+
First, some terminology and notational conventions:
828
+
829
+
- We use ´E´ as a name of an enum definition, and ´C´ as a name of an enum case that appears in ´E´.
830
+
- We use `<...>` for syntactic constructs that in some circumstances might be empty.
831
+
For instance, `<value-params>` represents one or more parameter lists `(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)` or nothing at all.
832
+
- Enum classes fall into two categories:
833
+
-_parameterized_ enum classes have at least one of the following:
834
+
- a type parameter section, denoted as `[´\mathit{tps}\,´]`;
835
+
- one or more (possibly empty) parameter sections, denoted as `(´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)`.
836
+
-_unparameterized_ enum classes have no type parameter sections and no parameter sections.
837
+
- Enum cases fall into three categories:
838
+
839
+
-_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´)`.
840
+
-_Simple cases_ are cases of an unparameterized enum that have neither parameters nor an extends clause or body.
841
+
That is, they consist of a name only.
842
+
-_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.
843
+
844
+
- Simple cases and value cases are collectively called _singleton cases_.
845
+
846
+
###### Example
847
+
848
+
An example enum for a `Planet` enumeration can be given as
Rules (4) to (6) define `extends` clauses for cases that are missing them.
899
+
Rules (7) to (9) define how such cases with `extends` clauses map into `case class`es or `val`s.
836
900
837
-
Simple enum cases all share a single implementation class.
901
+
1. An `enum` definition
902
+
```scala
903
+
enum ´E´ ... { <defs> <cases> }
904
+
```
905
+
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).
906
+
Theenumclass starts with a compiler-generated importthatimportsthenames`<caseIds>`ofallcasessothattheycanbeusedwithoutprefixintheclass.
907
+
```scala
908
+
sealedabstractclass ´E´ ... extends <parents> with scala.reflect.Enum {
909
+
import ´E´.{ <caseIds> }
910
+
<defs>
911
+
}
912
+
object ´E´ { <cases> }
913
+
```
914
+
915
+
2. A singleton case consisting of a comma-separated list of enumnames
916
+
```scala
917
+
case ´C_1´, ..., ´C_n´
918
+
```
919
+
expands to
920
+
```scala
921
+
case ´C_1´; ...; case ´C_n´
922
+
```
923
+
Any modifiers or annotations on the original case extend to all expanded cases.
924
+
This result is then further rewritten by either (3 or 4).
925
+
926
+
3. A singleton case without an extends clause
927
+
```scala
928
+
case ´C´
929
+
```
930
+
of an unparameterized enum `´E´` expands to the following simple enum case in `´E´`'s companion object:
931
+
```scala
932
+
val ´C´ = $new(n, "C")
933
+
```
934
+
Here, `$new` is a private method that creates an instance of ´E´ (see below).
where `´\mathit{n}´` is the ordinal number of the case in the companion object, starting from 0.
1007
+
**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>`.
1008
+
1009
+
###### Superclass of an enum case
1010
+
1011
+
an enum case (singleton or class) with explicit extends clause
1012
+
```scala
1013
+
case ´C´ <type-params> <value-params> extends <parents>
1014
+
```
838
1015
839
-
Value enum cases will each be implemented by a unique class.
1016
+
must extend the parent enum `´E´` as the first parent of `<parents>`.
840
1017
841
1018
###### Example
842
1019
Consider the enumeration `RGB`, consisting of simple enum cases:
@@ -845,12 +1022,18 @@ enum RGB:
845
1022
caseRed, Green, Blue
846
1023
```
847
1024
848
-
Each simple case `Red`, `Green`, and `Blue` will be instantiated using a private method that takes a tag and a name as arguments.
849
-
850
-
For instance, the first definition of value `Color.Red` above would expand to:
1025
+
The three simple cases will expand as follows in the companion of `RGB`:
851
1026
852
1027
```scala
853
-
valRed:Color= $new(0, "Red")
1028
+
valRed= $new(0, "Red")
1029
+
valGreen= $new(1, "Green")
1030
+
valBlue= $new(2, "Blue")
1031
+
1032
+
privatedef$new(_$ordinal: Int, $name: String) =
1033
+
newRGBwith scala.runtime.EnumValue:
1034
+
defordinal= _$ordinal
1035
+
overridedefproductPrefix= $name
1036
+
overridedeftoString= $name
854
1037
```
855
1038
856
1039
@@ -864,15 +1047,72 @@ enum Color(val rgb: Int):
864
1047
caseBlueextendsColor(0x0000FF)
865
1048
```
866
1049
867
-
Each value case will expand as follows, for instance `Green` expands to
1050
+
The three value cases will expand as follows in the companion of `Color`:
868
1051
869
1052
```scala
870
-
valGreen:Color=newColor(0x00FF00):
1053
+
valRed=newColor(0xFF0000):
1054
+
defordinal:Int=0
1055
+
overridedefproductPrefix:String="Red"
1056
+
overridedeftoString:String="Red"
1057
+
valGreen=newColor(0x00FF00):
871
1058
defordinal:Int=1
872
1059
overridedefproductPrefix:String="Green"
873
1060
overridedeftoString:String="Green"
1061
+
valBlue=newColor(0x0000FF):
1062
+
defordinal:Int=2
1063
+
overridedefproductPrefix:String="Blue"
1064
+
overridedeftoString:String="Blue"
874
1065
```
875
1066
1067
+
### Widening of enum cases post-construction
1068
+
The compiler-generated `apply` and `copy` methods of an class enum case
1069
+
```scala
1070
+
case ´C´[´\mathit{tps}\,´](´\mathit{ps}_1\,´)...(´\mathit{ps}_n´) extends ´P_1´, ..., ´P_n´
1071
+
```
1072
+
are treated specially.
1073
+
A call `´C´[´\mathit{tps}\,´](´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)` of the `apply` method is ascribed the underlying type `´P_1´ & ... & ´P_n´` (dropping any [transparent traits](../other-new-features/transparent-traits.md)) as long as that type is still compatible with the expected type at the point of application.
1074
+
A call `t.copy[´\mathit{tps}\,´](´\mathit{ps}_1\,´)...(´\mathit{ps}_n´)` of `´C´`'s `copy` method is treated in the same way.
1075
+
1076
+
### Translation of enums with only singleton cases
1077
+
1078
+
An enum `´E´` (possibly generic) that defines one or more singleton cases, and no class cases will define the following additional synthetic members in its companion object (where `´E'´` denotes `´E´` with any type parameters replaced by wildcards):
1079
+
1080
+
- A method `valueOf(name: String): ´E'´`.
1081
+
It returns the singleton case value whose identifier is `name`.
1082
+
- A method `values` which returns an `Array[´E'´]` of all singleton case values defined by `E`, in the order of their definitions.
1083
+
1084
+
### Factory method for simple enum cases
1085
+
1086
+
If an enum `´E´` contains at least one simple case, its companion object will define in addition:
1087
+
1088
+
- A private method `$new` which defines a new simple case value with given ordinal number and name.
1089
+
This method can be thought as being defined as follows.
1090
+
1091
+
```scala
1092
+
privatedef$new(_$ordinal: Int, $name: String): ´E´ with runtime.EnumValue
1093
+
```
1094
+
-`$new` returns a new instance of an anonymous class which implements the abstract `Product` methods that it inherits from `Enum`.
1095
+
- 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`.
1096
+
Additionally `productPrefix` will delegate to `this.name`.
1097
+
1098
+
### Translation of Java-compatible enums
1099
+
1100
+
A Java-compatible enum is an enum that extends `java.lang.Enum`.
1101
+
The translation rules are the same as above, with the reservations defined in this section.
1102
+
1103
+
- It is a compile-time error for a Java-compatible enum to have class cases.
1104
+
1105
+
- Cases such as `case C` expand to a `@static val` as opposed to a `val`.
1106
+
This allows them to be generated as static fields of the enum type, thus ensuring they are represented the same way as Java enums.
1107
+
1108
+
### Scopes for Enum Cases
1109
+
1110
+
A case in an `enum` is treated similarly to a secondary constructor.
1111
+
It can access neither the enclosing `enum` using `this`, nor its value parameters or instance members using simple identifiers.
1112
+
1113
+
Even though translated enum cases are located in the enum's companion object, referencing this object or its members via `this` or a simple identifier is also illegal.
1114
+
The compiler typechecks enum cases in the scope of the enclosing companion object but flags any such illegal accesses as errors.
1115
+
876
1116
### Variance for Type Parameters
877
1117
878
1118
A parameterized enum case ´C´ of enum ´E´ with _inferred_ type parameters will copy variance annotations.
0 commit comments