Skip to content

Commit 7e6869d

Browse files
Documentation fixes for enums
1 parent 405dbfb commit 7e6869d

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ object DesugarEnums {
122122

123123
/** A creation method for a value of enum type `E`, which is defined as follows:
124124
*
125-
* private def $new($tag: Int, $name: String) = new E {
126-
* def ordinal = $tag
125+
* private def $new(_$ordinal: Int, $name: String) = new E {
126+
* def $ordinal = $tag
127127
* override def toString = $name
128128
* $values.register(this)
129129
* }

docs/docs/reference/enums/desugarEnums.md

+13-14
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ map into case classes or vals.
120120

121121
expands to a value definition in `E`'s companion object:
122122

123-
val C = new <parents> { <body>; def enumTag = n; $values.register(this) }
123+
val C = new <parents> { <body>; def ordinal = n; $values.register(this) }
124124

125125
where `n` is the ordinal number of the case in the companion object,
126126
starting from 0. The statement `$values.register(this)` registers the value
127-
as one of the `enumValues` of the enumeration (see below). `$values` is a
127+
as one of the `values` of the enumeration (see below). `$values` is a
128128
compiler-defined private value in the companion object.
129129

130130
It is an error if a value case refers to a type parameter of the enclosing `enum`
@@ -140,10 +140,10 @@ map into case classes or vals.
140140

141141
However, unlike for a regular case class, the return type of the associated
142142
`apply` method is a fully parameterized type instance of the enum class `E`
143-
itself instead of `C`. Also the enum case defines an `enumTag` method of
143+
itself instead of `C`. Also the enum case defines an `ordinal` method of
144144
the form
145145

146-
def enumTag = n
146+
def ordinal = n
147147

148148
where `n` is the ordinal number of the case in the companion object,
149149
starting from 0.
@@ -159,12 +159,9 @@ Non-generic enums `E` that define one or more singleton cases
159159
are called _enumerations_. Companion objects of enumerations define
160160
the following additional members.
161161

162-
- A method `enumValue` of type `scala.collection.immutable.Map[Int, E]`.
163-
`enumValue(n)` returns the singleton case value with ordinal number `n`.
164-
- A method `enumValueNamed` of type `scala.collection.immutable.Map[String, E]`.
165-
`enumValueNamed(s)` returns the singleton case value whose `toString`
166-
representation is `s`.
167-
- A method `enumValues` which returns an `Iterable[E]` of all singleton case
162+
- A method `valueOf(name: String): E`. It returns the singleton case value whose
163+
`toString` representation is `name`.
164+
- A method `values` which returns an `Array[E]` of all singleton case
168165
values in `E`, in the order of their definitions.
169166

170167
Companion objects of enumerations that contain at least one simple case define in addition:
@@ -173,12 +170,14 @@ Companion objects of enumerations that contain at least one simple case define i
173170
ordinal number and name. This method can be thought as being defined as
174171
follows.
175172

176-
def $new(tag: Int, name: String): ET = new E {
177-
def enumTag = tag
178-
def toString = name
179-
$values.register(this) // register enum value so that `valueOf` and `values` can return it.
173+
private def $new(\_$ordinal: Int, $name: String) = new E {
174+
def $ordinal = $tag
175+
override def toString = $name
176+
$values.register(this) // register enum value so that `valueOf` and `values` can return it.
180177
}
181178

179+
The `$ordinal` method above is used to generate the `ordinal` method if the enum does not extend a `java.lang.Enum` (as Scala enums do not extend `java.lang.Enum`s unless explicitly specified). In case it does, there is no need to generate `ordinal` as `java.lang.Enum` defines it.
180+
182181
### Scopes for Enum Cases
183182

184183
A case in an `enum` is treated similarly to a secondary constructor. It can access neither the enclosing `enum` using `this`, nor its value parameters or instance members using simple

docs/docs/reference/enums/enums.md

+30-16
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,25 @@ explicit extends clause.
3333
### Methods defined for enums
3434

3535
The values of an enum correspond to unique integers. The integer
36-
associated with an enum value is returned by its `enumTag` method:
36+
associated with an enum value is returned by its `ordinal` method:
3737

3838
```scala
3939
scala> val red = Color.Red
4040
val red: Color = Red
41-
scala> red.enumTag
41+
scala> red.ordinal
4242
val res0: Int = 0
4343
```
4444

45-
The companion object of an enum also defines three utility methods.
46-
The `enumValue` and `enumValueNamed` methods obtain an enum value
47-
by its tag or its name. The `enumValues` method returns all enum values
48-
defined in an enumeration in an `Iterable`.
45+
The companion object of an enum also defines two utility methods.
46+
The `valueOf` method obtains an enum value
47+
by its name. The `values` method returns all enum values
48+
defined in an enumeration in an `Array`.
4949

5050
```scala
51-
scala> Color.enumValue(1)
52-
val res1: Color = Green
53-
scala> Color.enumValueNamed("Blue")
54-
val res2: Color = Blue
55-
scala> Color.enumValues
56-
val res3: collection.Iterable[Color] = MapLike(Red, Green, Blue)
51+
scala> Color.valueOf("Blue")
52+
val res0: Color = Blue
53+
scala> Color.values
54+
val res1: Array[Color] = Array(Red, Green, Blue)
5755
```
5856

5957
### User-defined members of enums
@@ -84,16 +82,32 @@ object Planet {
8482
def main(args: Array[String]) = {
8583
val earthWeight = args(0).toDouble
8684
val mass = earthWeight / Earth.surfaceGravity
87-
for (p <- enumValues)
85+
for (p <- values)
8886
println(s"Your weight on $p is ${p.surfaceWeight(mass)}")
8987
}
9088
}
9189
```
9290

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:
93+
94+
```scala
95+
enum Color extends compat.JEnum[Color] { case Red, Green, Blue }
96+
```
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`.
99+
100+
After defining `Color` like that, you can use like you would a Java enum:
101+
102+
```scala
103+
scala> Color.Red.compareTo(Color.Green)
104+
val res15: Int = -1
105+
```
106+
93107
### Implementation
94108

95109
Enums are represented as `sealed` classes that extend the `scala.Enum` trait.
96-
This trait defines a single method, `enumTag`:
110+
This trait defines a single public method, `ordinal`:
97111

98112
```scala
99113
package scala
@@ -102,7 +116,7 @@ package scala
102116
trait Enum {
103117

104118
/** A number uniquely identifying a case of an enum */
105-
def enumTag: Int
119+
def ordinal: Int
106120
}
107121
```
108122

@@ -112,7 +126,7 @@ For instance, the `Venus` value above would be defined like this:
112126
```scala
113127
val Venus: Planet =
114128
new Planet(4.869E24, 6051800.0) {
115-
def enumTag: Int = 1
129+
def ordinal: Int = 1
116130
override def toString: String = "Venus"
117131
// internal code to register value
118132
}

0 commit comments

Comments
 (0)