Skip to content
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

Make enums interop with Java enums #4088

Closed
odersky opened this issue Mar 9, 2018 · 3 comments
Closed

Make enums interop with Java enums #4088

odersky opened this issue Mar 9, 2018 · 3 comments
Assignees

Comments

@odersky
Copy link
Contributor

odersky commented Mar 9, 2018

There are two directions:

  1. Interpret Java enums as Scala enums
  2. Have Scala enums map to Java enums, so that they can be seen as native enums in Java code

See #1970 for comments about how this might be achieved.

@smarter
Copy link
Member

smarter commented Apr 10, 2018

  1. Interpret Java enums as Scala enums

We already implement exhaustivity checking for Java enums, not sure if there's anything else we need to do?

  1. Have Scala enums map to Java enums, so that they can be seen as native enums in Java code

The JVMS defines the classfile flag ACC_ENUM but does not ascribe any semantic to it. The JLS states that an enum E must extend java.lang.Enum<E>, and implement the following methods:

public static E[] values();
public static E valueOf(String name);

It also describes various limitations of enums, and here things are less clear: what happens if you have an enum that does not respect these limitations? Does javac crash? Do we get runtime errors when using java.lang.EnumSet, java.lang.EnumMap, or one of the enum-related method of java.lang.Class? This doesn't seem to be specified anywhere. To be on the safe side, we could make Dotty enums have the ENUM flag set and extend java.lang.Enum only if they respect all the limitations listed in the JLS. Or we could always emit it and see what happens. Note that there's a Java Enhancement Proposal for lifting some of the limitations of Java enums: http://openjdk.java.net/jeps/301 (spec diff: http://cr.openjdk.java.net/~dlsmith/enhanced-enums.html)

@smarter smarter mentioned this issue Nov 3, 2018
@nicolasstucki
Copy link
Contributor

@anatoliykmetyuk what is the current status of Java enums interop?

@anatoliykmetyuk
Copy link
Contributor

anatoliykmetyuk commented Jul 17, 2019

The current status is best described by this test. Basically, everything that you can do with Java enums can be done with Scala enums from Java. The only two known incompatibilities are #6663 which will be solved while solving #6620 (which implies that Scala enums require Scala library on classpath).

I'm closing this issue since the interop works in general and all the minor issues are discussed in separate tickets.

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

No branches or pull requests

4 participants