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
<!-- TODO: Could not find more info on which non-Product methods case class automatically define -->
346
346
All case classes automatically extend the `Product` trait (and generate synthetic methods to conform to it) (but not `Product´n´`), and define a `_´n´` method for each of their arguments.
347
347
348
+
### Trait `Enum`
349
+
<!-- TODO: Move somewhere else ? -->
350
+
All enum definitions automatically extend the `reflect.Enum` trait (and generate synthetic methods to conform to it).
351
+
348
352
### Class `Array`
349
353
350
354
All operations on arrays desugar to the corresponding operations of the underlying platform.
Copy file name to clipboardExpand all lines: docs/_spec/APPLIEDreference/enums/enums.md
+24-64Lines changed: 24 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -11,9 +11,8 @@ enum Color:
11
11
caseRed, Green, Blue
12
12
```
13
13
14
-
This defines a new `sealed` class, `Color`, with three values, `Color.Red`,
15
-
`Color.Green`, `Color.Blue`. The color values are members of `Color`s
16
-
companion object.
14
+
This defines a new `sealed` class, `Color`, with three values, `Color.Red`, `Color.Green`, `Color.Blue`.
15
+
The color values are members of `Color`s companion object.
17
16
18
17
## Parameterized enums
19
18
@@ -26,13 +25,12 @@ enum Color(val rgb: Int):
26
25
caseBlueextendsColor(0x0000FF)
27
26
```
28
27
29
-
As the example shows, you can define the parameter value by using an
30
-
explicit extends clause.
28
+
As the example shows, you can define the parameter value by using an explicit extends clause.
31
29
32
30
## Methods defined for enums
33
31
34
-
The values of an enum correspond to unique integers. The integer
35
-
associated with an enum value is returned by its `ordinal` method:
32
+
The values of an enum correspond to unique integers.
33
+
The integer associated with an enum value is returned by its `ordinal` method:
36
34
37
35
```scala
38
36
scala>valred=Color.Red
@@ -42,10 +40,9 @@ val res0: Int = 0
42
40
```
43
41
44
42
The companion object of an enum also defines three utility methods.
45
-
The `valueOf` method obtains an enum value
46
-
by its name. The `values` method returns all enum values
47
-
defined in an enumeration in an `Array`. The `fromOrdinal`
48
-
method obtains an enum value from its ordinal (`Int`) value.
43
+
The `valueOf` method obtains an enum value by its name.
44
+
The `values` method returns all enum values defined in an enumeration in an `Array`.
45
+
The `fromOrdinal` method obtains an enum value from its ordinal (`Int`) value.
49
46
50
47
```scala
51
48
scala>Color.valueOf("Blue")
@@ -58,7 +55,8 @@ val res2: Color = Red
58
55
59
56
## User-defined members of enums
60
57
61
-
It is possible to add your own definitions to an enum. Example:
58
+
It is possible to add your own definitions to an enum.
59
+
For example:
62
60
63
61
```scala
64
62
enumPlanet(mass: Double, radius: Double):
@@ -94,11 +92,10 @@ end Planet
94
92
95
93
Enum case declarations are similar to secondary constructors:
96
94
they are scoped outside of the enum template, despite being declared within it.
97
-
This means that enum case declarations cannot access inner members of the
98
-
enum class.
95
+
This means that enum case declarations cannot access inner members of the enum class.
99
96
100
-
Similarly, enum case declarations may not directly reference members of the enum's companion object,
101
-
even if they are imported (directly, or by renaming). For example:
97
+
Similarly, enum case declarations may not directly reference members of the enum's companion object, even if they are imported (directly, or by renaming).
The fields referenced by `Mercury` are not visible, and the fields referenced by `Venus` may not
117
-
be referenced directly (using `import Planet.*`). You must use an indirect reference,
118
-
such as demonstrated with `Earth`.
113
+
The fields referenced by `Mercury` are not visible, and the fields referenced by `Venus` may not be referenced directly (using `import Planet.*`).
114
+
You must use an indirect reference, such as demonstrated with `Earth`.
119
115
120
116
## Deprecation of Enum Cases
121
117
122
-
As a library author, you may want to signal that an enum case is no longer intended for use. However you could still want to gracefully handle the removal of a case from your public API, such as special casing deprecated cases.
118
+
As a library author, you may want to signal that an enum case is no longer intended for use.
119
+
However you could still want to gracefully handle the removal of a case from your public API, such as special casing deprecated cases.
123
120
124
121
To illustrate, say that the `Planet` enum originally had an additional case:
125
122
@@ -131,7 +128,8 @@ To illustrate, say that the `Planet` enum originally had an additional case:
131
128
end Planet
132
129
```
133
130
134
-
We now want to deprecate the `Pluto` case. First we add the `scala.deprecated` annotation to `Pluto`:
131
+
We now want to deprecate the `Pluto` case.
132
+
First we add the `scala.deprecated` annotation to `Pluto`:
135
133
136
134
```diff
137
135
enum Planet(mass: Double, radius: Double):
@@ -144,7 +142,8 @@ We now want to deprecate the `Pluto` case. First we add the `scala.deprecated` a
144
142
end Planet
145
143
```
146
144
147
-
Outside the lexical scopes of `enum Planet` or `object Planet`, references to `Planet.Pluto` will produce a deprecation warning, but within those scopes we can still reference it to implement introspection over the deprecated cases:
145
+
Outside the lexical scopes of `enum Planet` or `object Planet`, references to `Planet.Pluto` will produce a deprecation warning.
146
+
Within those scopes however, we can still reference it to implement introspection over the deprecated cases:
148
147
149
148
```scala
150
149
traitDeprecations[T<: reflect.Enum] {
@@ -163,8 +162,7 @@ We could imagine that a library may use [type class derivation](../contextual/de
163
162
164
163
## Compatibility with Java Enums
165
164
166
-
If you want to use the Scala-defined enums as [Java enums](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html), you can do so by extending
167
-
the class `java.lang.Enum`, which is imported by default, as follows:
165
+
If you want to use the Scala-defined enums as [Java enums](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html), you can do so by extending the class `java.lang.Enum`, which is imported by default, as follows:
168
166
169
167
```scala
170
168
enumColorextendsEnum[Color] { caseRed, Green, Blue }
For a more in-depth example of using Scala 3 enums from Java, see [this test](https://github.com/lampepfl/dotty/tree/main/tests/run/enum-java). In the test, the enums are defined in the `MainScala.scala` file and used from a Java source, `Test.java`.
184
-
185
-
## Implementation
186
-
187
-
Enums are represented as `sealed` classes that extend the `scala.reflect.Enum` trait.
188
-
This trait defines a single public method, `ordinal`:
0 commit comments