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
+13-14
Original file line number
Diff line number
Diff line change
@@ -120,11 +120,11 @@ map into case classes or vals.
120
120
121
121
expands to a value definition in `E`'s companion object:
122
122
123
-
val C = new <parents> { <body>; def enumTag = n; $values.register(this) }
123
+
val C = new <parents> { <body>; def ordinal = n; $values.register(this) }
124
124
125
125
where `n` is the ordinal number of the case in the companion object,
126
126
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
128
128
compiler-defined private value in the companion object.
129
129
130
130
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.
140
140
141
141
However, unlike for a regular case class, the return type of the associated
142
142
`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
144
144
the form
145
145
146
-
def enumTag = n
146
+
def ordinal = n
147
147
148
148
where `n` is the ordinal number of the case in the companion object,
149
149
starting from 0.
@@ -159,12 +159,9 @@ Non-generic enums `E` that define one or more singleton cases
159
159
are called _enumerations_. Companion objects of enumerations define
160
160
the following additional members.
161
161
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
168
165
values in `E`, in the order of their definitions.
169
166
170
167
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
173
170
ordinal number and name. This method can be thought as being defined as
174
171
follows.
175
172
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.
180
177
}
181
178
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
+
182
181
### Scopes for Enum Cases
183
182
184
183
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
println(s"Your weight on $p is ${p.surfaceWeight(mass)}")
89
87
}
90
88
}
91
89
```
92
90
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
+
enumColorextends compat.JEnum[Color] { caseRed, 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
+
valres15:Int=-1
105
+
```
106
+
93
107
### Implementation
94
108
95
109
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`:
97
111
98
112
```scala
99
113
packagescala
@@ -102,7 +116,7 @@ package scala
102
116
traitEnum {
103
117
104
118
/** A number uniquely identifying a case of an enum */
105
-
defenumTag:Int
119
+
defordinal:Int
106
120
}
107
121
```
108
122
@@ -112,7 +126,7 @@ For instance, the `Venus` value above would be defined like this:
0 commit comments