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

Java Enums cannot be used in case branches (but can in if statements) #7551

Closed
radeusgd opened this issue Aug 10, 2023 · 2 comments · Fixed by #7607
Closed

Java Enums cannot be used in case branches (but can in if statements) #7551

radeusgd opened this issue Aug 10, 2023 · 2 comments · Fixed by #7607
Assignees

Comments

@radeusgd
Copy link
Member

radeusgd commented Aug 10, 2023

Currently, if I try to pattern match a Java enum:

package foo;

import org.graalvm.polyglot.Value;

public enum Foo {
    A, B, C;

    public static Foo generate(int i) {
        if (i == 0) return A;
        return B;
    }
}
from Standard.Base import all

polyglot java import foo.Foo

convert enum_value =
    case enum_value of
        Foo.A -> "aaa"
        Foo.B -> "bbb"
        Foo.C -> "ccc"

main =
    IO.println (convert Foo.C)
    IO.println (convert (Foo.generate 0))
    IO.println (convert (Foo.generate 1))

I get

C:\NBO\repr\PolyProject\src\Main.enso: error: The name `Foo.A` could not be found.
Aborting due to 1 errors and 0 warnings.
Execution finished with an error: Compilation aborted due to errors.

I can convert the Enso code to:

from Standard.Base import all
import Standard.Base.Errors.Common.No_Such_Method

polyglot java import foo.Foo

convert enum_value =
    if enum_value == Foo.A then "aaa" else
        if enum_value == Foo.B then "bbb" else
            if enum_value == Foo.C then "ccc" else 
                Nothing

main =
    IO.println (convert Foo.C)
    IO.println (convert (Foo.generate 0))
    IO.println (convert (Foo.generate 1))

and then it works:

ccc
aaa
bbb

Nonetheless, it would be nice if the Java enums were also supported in regular pattern matching. It may be a bit surprising to users that if works but case does not.

Of course this is lower priority, because most of our users won't be using Java polyglot as much and even less will care about enums. But it may be worth to improve this at some point.

@radeusgd
Copy link
Member Author

Another small issue related to this:

As you can see the error message C:\NBO\repr\PolyProject\src\Main.enso: error: The name Foo.A could not be found. - it does not have any indication of the position of the error in the file. In my small example it is easy to find it, but in a big library file it may be problematic when it is unclear where the error is located.

@4e6 4e6 removed the triage label Aug 11, 2023
@JaroslavTulach JaroslavTulach changed the title Enso Interop could support pattern matching Java Enums Java Enums cannot be used in case branches (but can in if statements) Aug 13, 2023
@jdunkerley jdunkerley moved this from ❓New to 📤 Backlog in Issues Board Aug 15, 2023
@JaroslavTulach JaroslavTulach moved this from 📤 Backlog to 👁️ Code review in Issues Board Aug 18, 2023
@mergify mergify bot closed this as completed in #7607 Aug 21, 2023
mergify bot pushed a commit that referenced this issue Aug 21, 2023
Fixes #7551 by looking up static symbols of a `JavaClass` polyglot object.
@github-project-automation github-project-automation bot moved this from 👁️ Code review to 🟢 Accepted in Issues Board Aug 21, 2023
@enso-bot
Copy link

enso-bot bot commented Aug 22, 2023

Jaroslav Tulach reports a new STANDUP for yesterday (2023-08-21):

Progress: - JavaEnums merged: b9702d1

Next Day: More of Python Interop & TCK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants