Skip to content

Java style enums are accessed from the companion object #9959

Open
@bishabosha

Description

@bishabosha

Minimized code

enum Color extends java.lang.Enum[Color] {
  case Red, Green, Blue
}

@main def Test = println(Color.Red)

Output

// bytecode for Test
public void Test();
    Code:
       0: getstatic     #31                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
       3: getstatic     #36                 // Field Color$.MODULE$:LColor$;
       6: pop
       7: getstatic     #40                 // Field Color$.Red:LColor;
      10: invokevirtual #44                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      13: return

which is equivalent to println(Color$.Red)

Expectation

public void Test() {
  scala.Predef$.MODULE$.println(Color.Red);
}

this means that clients cannot swap out Color for an enum compiled by Java

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions