Skip to content

Should we remove scala.Enum trait as a parent of enums? #9873

Closed
@bishabosha

Description

@bishabosha

This issue poses the question of do we remove this trait, but keep the same API for individual enum classes, e.g. the following would still work after the change:

Minimized example

enum Colour extends java.lang.Enum[Colour]:
  case Red, Green, Blue

@main def Test = 
  println(Colour.Red.enumLabel)
  println(Colour.Green.ordinal)
  println(Colour.Blue.isInstanceOf[scala.Enum])

Output

Red
1
false

Points in Favour

  • If we remove the trait, then there is less overhead in the library, and there is no need to fully qualify java.lang.Enum
  • we still have Mirror framework which can be used to extract the ordinal generically

Points Against

  • currently in desugaring there is no way to determine in the statements of abstract class Colour if it derives from java.lang.Enum (Edit: alternatively it is possible to special case some specific desugarings to occur after parents are known)
  • therefore we can not avoid defining abstract def ordinal: Int when the parent is java.lang.Enum, leading to override errors
  • Mirror framework does not have generic way to fetch enumLabel
  • scala.Enum could be renamed (and remain a parent) so that java.lang.Enum parent does not need to be fully qualified

Considerations

  • perhaps the ordinal method should be renamed to avoid clashes with java.lang.Enum
  • perhaps we generate no public ordinal instance method and advise to use the ordinal method on the companion object. (same for enumLabel?)
  • perhaps revise Mirror framework to have a label method for Mirror.Sum

cc @odersky @smarter @sjrd

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions