Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoliykmetyuk committed Jun 8, 2019
1 parent 8e2a070 commit 229c451
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 10 additions & 2 deletions docs/docs/reference/enums/desugarEnums.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ Companion objects of enumerations that contain at least one simple case define i
ordinal number and name. This method can be thought as being defined as
follows.

private def $new(\_$ordinal: Int, $name: String) = new E {
def $ordinal = $tag
private def $new(_$ordinal: Int, $name: String) = new E {
def $ordinal = $_ordinal
override def toString = $name
$values.register(this) // register enum value so that `valueOf` and `values` can return it.
}
Expand All @@ -186,6 +186,14 @@ identifiers.
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. The compiler typechecks enum cases in the scope of the enclosing companion object but flags any such illegal accesses as errors.

### Translation of Java-compatible enums
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.

It is a compile-time error for a Java-compatible enum to have class cases.

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.


### Other Rules

A normal case class which is not produced from an enum case is not allowed to extend
Expand Down
8 changes: 5 additions & 3 deletions docs/docs/reference/enums/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ object Planet {
```

### Compatibility with Java Enums
If you want to use the Scala-defined enums as Java enums, you can do so by extending `compat.JEnum` class as follows:
If you want to use the Scala-defined enums as Java enums, you can do so by extending `java.lang.Enum` class as follows:

```scala
enum Color extends compat.JEnum[Color] { case Red, Green, Blue }
enum Color extends java.lang.Enum[Color] { case Red, Green, Blue }
```

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

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

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

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

### Implementation

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

0 comments on commit 229c451

Please sign in to comment.