Skip to content

Commit 4717553

Browse files
bishaboshasjrd
authored andcommitted
apply enums/enums.md
1 parent 8be388a commit 4717553

File tree

3 files changed

+80
-64
lines changed

3 files changed

+80
-64
lines changed

docs/_spec/05-classes-and-objects.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,58 @@ EnumCase ::= ‘case’ (id ClassConstr [‘extends’ ConstrApps] | ids)
821821

822822
<!-- TODO: an enum case is defined in terms of other scala constructs... -->
823823

824+
### Lowering of Enum Definitions
825+
826+
Enums are represented as `sealed` classes that extend the `scala.reflect.Enum` trait.
827+
This trait defines a single public method, `ordinal`:
828+
829+
```scala
830+
package scala.reflect
831+
832+
transparent trait Enum extends Any, Product, Serializable:
833+
834+
def ordinal: Int
835+
```
836+
837+
Simple enum cases all share a single implementation class.
838+
839+
Value enum cases will each be implemented by a unique class.
840+
841+
###### Example
842+
Consider the enumeration `RGB`, consisting of simple enum cases:
843+
```scala
844+
enum RGB:
845+
case Red, Green, Blue
846+
```
847+
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:
851+
852+
```scala
853+
val Red: Color = $new(0, "Red")
854+
```
855+
856+
857+
###### Example
858+
859+
Consider the more complex enumeration `Color`, consisting of value enum cases:
860+
```scala
861+
enum Color(val rgb: Int):
862+
case Red extends Color(0xFF0000)
863+
case Green extends Color(0x00FF00)
864+
case Blue extends Color(0x0000FF)
865+
```
866+
867+
Each value case will expand as follows, for instance `Green` expands to
868+
869+
```scala
870+
val Green: Color = new Color(0x00FF00):
871+
def ordinal: Int = 1
872+
override def productPrefix: String = "Green"
873+
override def toString: String = "Green"
874+
```
875+
824876
### Variance for Type Parameters
825877

826878
A parameterized enum case ´C´ of enum ´E´ with _inferred_ type parameters will copy variance annotations.

docs/_spec/12-the-scala-standard-library.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ class PartialFunction[-A, +B] extends Function1[A, B] {
345345
<!-- TODO: Could not find more info on which non-Product methods case class automatically define -->
346346
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.
347347

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+
348352
### Class `Array`
349353

350354
All operations on arrays desugar to the corresponding operations of the underlying platform.

docs/_spec/TODOreference/enums/enums.md renamed to docs/_spec/APPLIEDreference/enums/enums.md

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ enum Color:
1111
case Red, Green, Blue
1212
```
1313

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.
1716

1817
## Parameterized enums
1918

@@ -26,13 +25,12 @@ enum Color(val rgb: Int):
2625
case Blue extends Color(0x0000FF)
2726
```
2827

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.
3129

3230
## Methods defined for enums
3331

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:
3634

3735
```scala
3836
scala> val red = Color.Red
@@ -42,10 +40,9 @@ val res0: Int = 0
4240
```
4341

4442
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.
4946

5047
```scala
5148
scala> Color.valueOf("Blue")
@@ -58,7 +55,8 @@ val res2: Color = Red
5855

5956
## User-defined members of enums
6057

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:
6260

6361
```scala
6462
enum Planet(mass: Double, radius: Double):
@@ -94,11 +92,10 @@ end Planet
9492

9593
Enum case declarations are similar to secondary constructors:
9694
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.
9996

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).
98+
For example:
10299

103100
```scala
104101
import Planet.*
@@ -113,13 +110,13 @@ object Planet:
113110
private final val (earthMass, earthRadius) = (5.976e+24, 6.37814e6)
114111
end Planet
115112
```
116-
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`.
119115

120116
## Deprecation of Enum Cases
121117

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.
123120

124121
To illustrate, say that the `Planet` enum originally had an additional case:
125122

@@ -131,7 +128,8 @@ To illustrate, say that the `Planet` enum originally had an additional case:
131128
end Planet
132129
```
133130

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`:
135133

136134
```diff
137135
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
144142
end Planet
145143
```
146144

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:
148147

149148
```scala
150149
trait Deprecations[T <: reflect.Enum] {
@@ -163,8 +162,7 @@ We could imagine that a library may use [type class derivation](../contextual/de
163162

164163
## Compatibility with Java Enums
165164

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:
168166

169167
```scala
170168
enum Color extends Enum[Color] { case Red, Green, Blue }
@@ -180,43 +178,5 @@ scala> Color.Red.compareTo(Color.Green)
180178
val res15: Int = -1
181179
```
182180

183-
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`:
189-
190-
```scala
191-
package scala.reflect
192-
193-
/** A base trait of all Scala enum definitions */
194-
transparent trait Enum extends Any, Product, Serializable:
195-
196-
/** A number uniquely identifying a case of an enum */
197-
def ordinal: Int
198-
```
199-
200-
Enum values with `extends` clauses get expanded to anonymous class instances.
201-
For instance, the `Venus` value above would be defined like this:
202-
203-
```scala
204-
val Venus: Planet = new Planet(4.869E24, 6051800.0):
205-
def ordinal: Int = 1
206-
override def productPrefix: String = "Venus"
207-
override def toString: String = "Venus"
208-
```
209-
210-
Enum values without `extends` clauses all share a single implementation
211-
that can be instantiated using a private method that takes a tag and a name as arguments.
212-
For instance, the first
213-
definition of value `Color.Red` above would expand to:
214-
215-
```scala
216-
val Red: Color = $new(0, "Red")
217-
```
218-
219-
## Reference
220-
221-
For more information, see [Issue #1970](https://github.com/lampepfl/dotty/issues/1970) and
222-
[PR #4003](https://github.com/lampepfl/dotty/pull/4003).
181+
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).
182+
In the test, the enums are defined in the `MainScala.scala` file and used from a Java source, `Test.java`.

0 commit comments

Comments
 (0)