Skip to content

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

Merged
merged 10 commits into from
Aug 18, 2020

Conversation

bishabosha
Copy link
Member

@bishabosha bishabosha commented Aug 12, 2020

$ordinal only existed so that ordinal could be later added if the implementation did not extend java.lang.Enum, however we can check at desugaring the parents of the enum class and avoid defining ordinal in a concrete implementation of jl.Enum,

secondly, we can also remove $ordinal and toString override in concrete cases extending jl.Enum to avoid unnecessary field creation

@bishabosha bishabosha force-pushed the topic/enum-reduce-bytecode branch 2 times, most recently from d3af2e1 to db9029c Compare August 12, 2020 21:09
@bishabosha bishabosha marked this pull request as ready for review August 12, 2020 21:14
@bishabosha bishabosha requested review from odersky and smarter August 12, 2020 21:14
@smarter smarter assigned bishabosha and unassigned smarter Aug 14, 2020
@bishabosha
Copy link
Member Author

Here is a sketch for a cleaner implementation

enum Tag[T] extends java.lang.Enum[Tag[_]]:
  case IntTag extends Tag[Int]

previously, IntTag would desugar to:

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 {}

@smarter
Copy link
Member

smarter commented Aug 16, 2020

That seems like quite a lot of indirection, could completeJavaEnums compute the ordinal value itself?

@bishabosha bishabosha force-pushed the topic/enum-reduce-bytecode branch from db9029c to 93fc31c Compare August 17, 2020 10:57
@bishabosha bishabosha assigned smarter and unassigned bishabosha Aug 17, 2020
@bishabosha bishabosha requested a review from smarter August 17, 2020 11:20
@bishabosha
Copy link
Member Author

@smarter thanks for the tip, I now use a reverse lookup of the enum class children

@bishabosha bishabosha force-pushed the topic/enum-reduce-bytecode branch from 6662556 to 84ef267 Compare August 17, 2020 16:28
@bishabosha
Copy link
Member Author

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

Copy link
Member

@smarter smarter left a 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).

@bishabosha
Copy link
Member Author

bishabosha commented Aug 18, 2020

Is the issue that we can have a mix of simple and non-simple cases?

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

@bishabosha bishabosha force-pushed the topic/enum-reduce-bytecode branch 2 times, most recently from 53a9133 to 19cf4e9 Compare August 18, 2020 09:13
@bishabosha bishabosha force-pushed the topic/enum-reduce-bytecode branch from 19cf4e9 to 8bdae8e Compare August 18, 2020 09:25
@bishabosha bishabosha requested a review from smarter August 18, 2020 11:42
@bishabosha bishabosha merged commit 1821d22 into scala:master Aug 18, 2020
@bishabosha bishabosha deleted the topic/enum-reduce-bytecode branch August 18, 2020 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants