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/enums/desugarEnums.md
+10-2Lines changed: 10 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -170,8 +170,8 @@ Companion objects of enumerations that contain at least one simple case define i
170
170
ordinal number and name. This method can be thought as being defined as
171
171
follows.
172
172
173
-
private def $new(\_$ordinal: Int, $name: String) = new E {
174
-
def $ordinal = $tag
173
+
private def $new(_$ordinal: Int, $name: String) = new E {
174
+
def $ordinal = $_ordinal
175
175
override def toString = $name
176
176
$values.register(this) // register enum value so that `valueOf` and `values` can return it.
177
177
}
@@ -186,6 +186,14 @@ identifiers.
186
186
Even though translated enum cases are located in the enum's companion object, referencing
187
187
this object or its members via `this` or a simple identifier is also illegal. The compiler typechecks enum cases in the scope of the enclosing companion object but flags any such illegal accesses as errors.
188
188
189
+
### Translation of Java-compatible enums
190
+
A Java-compatible enum is an enum that extends `java.lang.Enum`. The translation rules are the same as above, with the reservations defined in this section.
191
+
192
+
It is a compile-time error for a Java-compatible enum to have class cases.
193
+
194
+
Cases such as `case C` expand to a `@static val` as opposed to a `val`. This allows them to be generated as static fields of the enum type, thus ensuring they are represented the same way as Java enums.
195
+
196
+
189
197
### Other Rules
190
198
191
199
A normal case class which is not produced from an enum case is not allowed to extend
Copy file name to clipboardExpand all lines: docs/docs/reference/enums/enums.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,13 +89,13 @@ object Planet {
89
89
```
90
90
91
91
### Compatibility with Java Enums
92
-
If you want to use the Scala-defined enums as Java enums, you can do so by extending `compat.JEnum` class as follows:
92
+
If you want to use the Scala-defined enums as Java enums, you can do so by extending `java.lang.Enum` class as follows:
93
93
94
94
```scala
95
-
enumColorextendscompat.JEnum[Color] { caseRed, Green, Blue }
95
+
enumColorextendsjava.lang.Enum[Color] { caseRed, Green, Blue }
96
96
```
97
97
98
-
The type parameter comes from the Java enum [definition](https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/Enum.html) and should me the same as the type of the enum. The compiler will transform the definition above so that `Color` extends `java.lang.Enum`.
98
+
The type parameter comes from the Java enum [definition](https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/Enum.html) and should be the same as the type of the enum. There is no need to provide constructor arguments (as defined in the API docs) to `java.lang.Enum` when extending it – the compiler will generate them automatically.
99
99
100
100
After defining `Color` like that, you can use like you would a Java enum:
For a more in-depth example of using Scala 3 enums from Java, see [this test](https://github.com/lampepfl/dotty/tree/master/tests/run/enum-java). In the test, the enums are defined in the `MainScala.scala` file and used from a Java source, `Test.java`.
108
+
107
109
### Implementation
108
110
109
111
Enums are represented as `sealed` classes that extend the `scala.Enum` trait.
0 commit comments