Skip to content

Commit a4e5911

Browse files
Scala Enum conforms to Java enum interface
1 parent fd20cbf commit a4e5911

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ object desugar {
593593
else if (isObject)
594594
parents1 = parents1 :+ scalaDot(nme.Serializable.toTypeName)
595595
if (isEnum)
596-
parents1 = parents1 :+ ref(defn.EnumType)
596+
parents1 = parents1 :+ appliedRef(ref(defn.EnumType), List(cdef))
597597

598598
// derived type classes of non-module classes go to their companions
599599
val (clsDerived, companionDerived) =

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ class Definitions {
599599
lazy val JavaSerializableClass: ClassSymbol = ctx.requiredClass("java.io.Serializable")
600600

601601
lazy val ComparableClass: ClassSymbol = ctx.requiredClass("java.lang.Comparable")
602+
lazy val Comparable_compareToR: TermRef = ComparableClass.requiredMethodRef(nme.compareTo)
603+
def Comparable_compareTo(implicit ctx: Context): Symbol = Comparable_compareToR.symbol
602604
lazy val SystemClass: ClassSymbol = ctx.requiredClass("java.lang.System")
603605
lazy val SystemModule: Symbol = SystemClass.linkedClass
604606

@@ -677,8 +679,6 @@ class Definitions {
677679
def Enum_getDeclaringClass(implicit ctx: Context): Symbol = Enum_getDeclaringClassR.symbol
678680
lazy val Enum_ordinalR: TermRef = EnumClass.requiredMethodRef(nme.ordinal)
679681
def Enum_ordinal(implicit ctx: Context): Symbol = Enum_ordinalR.symbol
680-
lazy val Enum_compareToR: TermRef = EnumClass.requiredMethodRef(nme.compareTo)
681-
def Enum_compareTo(implicit ctx: Context): Symbol = Enum_compareToR.symbol
682682

683683
lazy val EnumValuesType: TypeRef = ctx.requiredClassRef("scala.runtime.EnumValues")
684684
def EnumValuesClass(implicit ctx: Context): ClassSymbol = EnumValuesType.symbol.asClass

compiler/src/dotty/tools/dotc/transform/SyntheticMethods.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
4545
if (myValueSymbols.isEmpty) {
4646
myValueSymbols = List(defn.Any_hashCode, defn.Any_equals)
4747
mySimpleEnumCaseSymbols = List(
48-
defn.Enum_name, defn.Enum_ordinal, defn.Enum_compareTo, defn.Enum_getDeclaringClass)
48+
defn.Enum_name, defn.Enum_ordinal, defn.Comparable_compareTo, defn.Enum_getDeclaringClass)
4949
myCaseSymbols = myValueSymbols ++ mySimpleEnumCaseSymbols ++ List(defn.Any_toString, defn.Product_canEqual,
5050
defn.Product_productArity, defn.Product_productPrefix, defn.Product_productElement)
5151
myCaseModuleSymbols = myCaseSymbols.filter(_ ne defn.Any_equals)
@@ -80,11 +80,18 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
8080
else Nil
8181
}
8282

83+
def removeColors(msg: String): String = msg.replaceAll("\u001b\\[.*?m", "")
8384
def syntheticDef(sym: Symbol): Tree = {
84-
val synthetic = sym.copy(
85-
owner = clazz,
86-
flags = sym.flags &~ Deferred | Synthetic | Override,
87-
coord = clazz.coord).enteredAfter(thisPhase).asTerm
85+
// if (removeColors(sym.show) == "method getDeclaringClass")
86+
// println(s"Sym ${sym.show} info ${sym.denot.info.show} asf ${clazz.info.parents.head.show}: " +
87+
// s"${sym.denot.asSeenFrom(clazz.info.parents.head).info.show}")
88+
val synthetic = sym
89+
.copy(
90+
owner = clazz,
91+
info = if ((sym eq defn.Enum_getDeclaringClass) || (sym eq defn.Comparable_compareTo))
92+
sym.denot.asSeenFrom(clazz.info.parents.head).info else sym.info,
93+
flags = sym.flags &~ Deferred | Synthetic | Override,
94+
coord = clazz.coord).enteredAfter(thisPhase).asTerm
8895

8996
def forwardToRuntime(vrefss: List[List[Tree]]): Tree =
9097
ref(defn.runtimeMethodRef("_" + sym.name.toString)).appliedToArgs(This(clazz) :: vrefss.head)

library/src/scala/Enum.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package scala
22

33
/** A base trait of all enum classes */
4-
trait Enum {
5-
def getDeclaringClass: Class[_]
4+
trait Enum[E <: Enum[E]] extends Comparable[E] {
5+
def getDeclaringClass: Class[E]
66
def name: String
77
def ordinal: Int
8-
def compareTo(that: Enum): Int
9-
/** A number uniquely identifying a case of an enum */
10-
// def enumTag: Int
11-
// def name: String
128
}

library/src/scala/runtime/EnumValues.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scala.runtime
22

33
import scala.collection.immutable.Map
44

5-
class EnumValues[E <: Enum] {
5+
class EnumValues[E <: Enum[E]] {
66
private[this] var myMap: Map[Int, E] = Map()
77
private[this] var fromNameCache: Map[String, E] = null
88

0 commit comments

Comments
 (0)