Skip to content

Commit 25fd2e3

Browse files
committed
add enumLabel in terms of productPrefix
1 parent 671a50c commit 25fd2e3

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

docs/docs/reference/enums/enums.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,17 @@ For a more in-depth example of using Scala 3 enums from Java, see [this test](ht
110110
### Implementation
111111

112112
Enums are represented as `sealed` classes that extend the `scala.Enum` trait.
113-
This trait defines a single public method, `ordinal`:
113+
This trait defines a two public methods, `ordinal` and `enumLabel`:
114114

115115
```scala
116116
package scala
117117

118118
/** A base trait of all enum classes */
119119
trait Enum extends Product with Serializable {
120120

121+
/** A string uniquely identifying a case of an enum */
122+
final def enumLabel: String = productPrefix
123+
121124
/** A number uniquely identifying a case of an enum */
122125
def ordinal: Int
123126
}

library/src-bootstrapped/scala/Enum.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ package scala
33
/** A base trait of all enum classes */
44
trait Enum extends Product, Serializable:
55

6+
/** A string uniquely identifying a case of an enum */
7+
final def enumLabel: String = productPrefix
8+
69
/** A number uniquely identifying a case of an enum */
710
def ordinal: Int

library/src/scala/runtime/EnumValues.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ class EnumValues[E <: Enum] {
1414

1515
def fromInt: Map[Int, E] = myMap
1616
def fromName: Map[String, E] = {
17-
// TODO remove cast when scala.Enum is bootstrapped
18-
if (fromNameCache == null) fromNameCache = myMap.values.map(v => v.asInstanceOf[Product].productPrefix -> v).toMap
17+
if (fromNameCache == null) fromNameCache = myMap.values.map(v => v.productPrefix -> v).toMap
1918
fromNameCache
2019
}
2120
def values: Iterable[E] = myMap.values

tests/run/enum-custom-toString.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,33 @@ object Tag:
3434
@main def Test =
3535
assert(ES.A.toString == "overridden", s"ES.A.toString = ${ES.A.toString}")
3636
assert(ES.A.productPrefix == "A", s"ES.A.productPrefix = ${ES.A.productPrefix}")
37+
assert(ES.A.enumLabel == "A", s"ES.A.enumLabel = ${ES.A.enumLabel}")
3738
assert(ES.valueOf("A") == ES.A, s"ES.valueOf(A) = ${ES.valueOf("A")}")
3839
assert(EJ.B.toString == "overridden", s"EJ.B.toString = ${EJ.B.toString}")
3940
assert(EJ.B.productPrefix == "B", s"EJ.B.productPrefix = ${EJ.B.productPrefix}")
41+
assert(EJ.B.enumLabel == "B", s"EJ.B.enumLabel = ${EJ.B.enumLabel}")
4042
assert(EJ.valueOf("B") == EJ.B, s"EJ.valueOf(B) = ${EJ.valueOf("B")}")
4143
assert(EM.C.toString == "overridden", s"EM.C.toString = ${EM.C.toString}")
4244
assert(EM.C.productPrefix == "C", s"EM.C.productPrefix = ${EM.C.productPrefix}")
45+
assert(EM.C.enumLabel == "C", s"EM.C.enumLabel = ${EM.C.enumLabel}")
4346
assert(EM.valueOf("C") == EM.C, s"EM.valueOf(C) = ${EM.valueOf("C")}")
4447
assert(ET.D.toString == "overridden", s"ET.D.toString = ${ET.D.toString}")
4548
assert(ET.D.productPrefix == "D", s"ET.D.productPrefix = ${ET.D.productPrefix}")
49+
assert(ET.D.enumLabel == "D", s"ET.D.enumLabel = ${ET.D.enumLabel}")
4650
assert(EZ.E(0).toString == "overridden", s"EZ.E(0).toString = ${EZ.E(0).toString}")
4751
assert(EZ.E(0).productPrefix == "E", s"EZ.E(0).productPrefix = ${EZ.E(0).productPrefix}")
52+
assert(EZ.E(0).enumLabel == "E", s"EZ.E(0).enumLabel = ${EZ.E(0).enumLabel}")
4853
assert(EC.F.toString == "F", s"EC.F.toString = ${EC.F.toString}")
4954
assert(EC.F.productPrefix == "F", s"EC.F.productPrefix = ${EC.F.productPrefix}")
55+
assert(EC.F.enumLabel == "F", s"EC.F.enumLabel = ${EC.F.enumLabel}")
5056
assert(EC.valueOf("F") == EC.F, s"EC.valueOf(F) = ${EC.valueOf("F")}")
5157
assert(EC.G(0).toString == "G(0)", s"EC.G(0).toString = ${EC.G(0).toString}")
5258
assert(EC.G(0).productPrefix == "G", s"EC.G(0).productPrefix = ${EC.G(0).productPrefix}")
59+
assert(EC.G(0).enumLabel == "G", s"EC.G(0).enumLabel = ${EC.G(0).enumLabel}")
5360

5461
assert(
5562
assertion = Tag.IntTag.toString == s"${Tag.IntTag.getClass.getName}@${Integer.toHexString(123)}",
5663
message = s"Tag.IntTag.toString = ${Tag.IntTag.toString}"
5764
)
5865
assert(Tag.IntTag.productPrefix == Tag.IntTag.toString, s"Tag.IntTag.productPrefix = ${Tag.IntTag.productPrefix}")
66+
assert(Tag.IntTag.enumLabel == Tag.IntTag.toString, s"Tag.IntTag.enumLabel = ${Tag.IntTag.enumLabel}")

0 commit comments

Comments
 (0)