Skip to content

Commit 229c451

Browse files
Update docs
1 parent 8e2a070 commit 229c451

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

docs/docs/reference/enums/desugarEnums.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ Companion objects of enumerations that contain at least one simple case define i
170170
ordinal number and name. This method can be thought as being defined as
171171
follows.
172172

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
175175
override def toString = $name
176176
$values.register(this) // register enum value so that `valueOf` and `values` can return it.
177177
}
@@ -186,6 +186,14 @@ identifiers.
186186
Even though translated enum cases are located in the enum's companion object, referencing
187187
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.
188188

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+
189197
### Other Rules
190198

191199
A normal case class which is not produced from an enum case is not allowed to extend

docs/docs/reference/enums/enums.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ object Planet {
8989
```
9090

9191
### 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:
9393

9494
```scala
95-
enum Color extends compat.JEnum[Color] { case Red, Green, Blue }
95+
enum Color extends java.lang.Enum[Color] { case Red, Green, Blue }
9696
```
9797

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

100100
After defining `Color` like that, you can use like you would a Java enum:
101101

@@ -104,6 +104,8 @@ scala> Color.Red.compareTo(Color.Green)
104104
val res15: Int = -1
105105
```
106106

107+
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+
107109
### Implementation
108110

109111
Enums are represented as `sealed` classes that extend the `scala.Enum` trait.

0 commit comments

Comments
 (0)