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

Unable to use EnumSet in JSweet 2 #62

Open
joelmo opened this issue Mar 23, 2021 · 3 comments
Open

Unable to use EnumSet in JSweet 2 #62

joelmo opened this issue Mar 23, 2021 · 3 comments

Comments

@joelmo
Copy link

joelmo commented Mar 23, 2021

I have this code that gives me this error, using EnumMap also gives a similar error:
ERROR: type 'Permission' does not satisfy the constraint 'Enum<Permission>' at src\shared\Service.java(7)

Here is the code.

Service.java:
package shared;

import java.util.EnumSet;

public interface Service {

    public void setPermissions(EnumSet<Permission> permissions);

}

...

Permission.java:
package shared;

public enum Permission {
    READ, WRITE
}
@lgrignon
Copy link
Collaborator

@joelmo Thanks for reporting. Not that easy to fix.
The quickest workaround would be to loosen the generic constraint here https://github.com/j4ts/j4ts/blob/master/src/main/java/java/util/EnumSet.java#L33 to avoid extends Enum if you want to give it a try

@joelmo
Copy link
Author

joelmo commented Apr 11, 2021

I tried to find where that constraint is enforced in jsweet but didn't find where. The error message is confusing. Maybe jsweet doesn't translate enums into Enum<T?

@lgrignon
Copy link
Collaborator

That constraint is enforced by TypeScript compiler (tsc).
Indeed, it's specified in Java definition
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E> {
Hence will be transpiled as a TS generic constraint.

The problem is that your enum is transpiled to a standard TS enum which does not extend Enum of course (not possible in TS by the way). I recommend you try with this contraint removed. It's not a major one.

That being said, it could be nice that JSweet deals with this specificity when using J4TS, but it would require a non negligible amount of work.

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

No branches or pull requests

2 participants