-
Notifications
You must be signed in to change notification settings - Fork 1.1k
remove dollar ordinal from Enum #9539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove dollar ordinal from Enum #9539
Conversation
d3af2e1
to
db9029c
Compare
Here is a sketch for a cleaner implementation enum Tag[T] extends java.lang.Enum[Tag[_]]:
case IntTag extends Tag[Int] previously, final case <enum> val IntTag: Tag[Int] =
new Tag[Int] with runtime.EnumValue {
override def toString: String = "IntTag"
override def $ordinal: Int = 0
} and after completeJavaEnums become: final case <enum> val IntTag: Tag[Int] =
new Tag[Int]("IntTag", 0) with runtime.EnumValue {
override def toString: String = "IntTag"
override def $ordinal: Int = 0
} instead we can do: @annotation.internal.ValueEnumCase("IntTag", 0)
final case <enum> val IntTag: Tag[Int] =
new Tag[Int] with runtime.EnumValue {} which after completeJavaEnums become: @annotation.internal.ValueEnumCase("IntTag", 0)
final case <enum> val IntTag: Tag[Int] =
new Tag[Int]("IntTag", 0) with runtime.EnumValue {} |
That seems like quite a lot of indirection, could completeJavaEnums compute the ordinal value itself? |
db9029c
to
93fc31c
Compare
@smarter thanks for the tip, I now use a reverse lookup of the enum class children |
compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala
Outdated
Show resolved
Hide resolved
compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala
Outdated
Show resolved
Hide resolved
compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala
Outdated
Show resolved
Hide resolved
6662556
to
84ef267
Compare
There doesn't seem to be an optimal way of building the map of case -> ordinal that ignores simple enum cases, unless it was prepared in an earlier stage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There doesn't seem to be an optimal way of building the map of case -> ordinal that ignores simple enum cases
Is the issue that we can have a mix of simple and non-simple cases? If so, do we have testcases that check that enums with mixes like this are correctly generated (ordinal
behaves as expected on all values, etc).
compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala
Outdated
Show resolved
Hide resolved
compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala
Outdated
Show resolved
Hide resolved
That is correct, if there was no way to mix them then the branch for companions could be removed, and I'll add a test with mixing cases |
53a9133
to
19cf4e9
Compare
19cf4e9
to
8bdae8e
Compare
$ordinal
only existed so thatordinal
could be later added if the implementation did not extendjava.lang.Enum
, however we can check at desugaring the parents of the enum class and avoid definingordinal
in a concrete implementation ofjl.Enum
,secondly, we can also remove
$ordinal
andtoString
override in concrete cases extendingjl.Enum
to avoid unnecessary field creation