-
Notifications
You must be signed in to change notification settings - Fork 23
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
@JsEnum types cannot be used in a union #40
Comments
Riffing on this idea a bit more:
This could be handy too, in cases like
where the string keys don't match the enum name or the ordinal doesn't match the value, so you can either do the "proper" java thing and use |
IIRC, we discussed earlier that externs should not have enum definitions. if we are not handling the correctly we should probably have a good warning about it. I think Closure is willing to accept patches for converting such enums to number. @jDramaix please correct me if I'm wrong. |
Perhaps jsinterop-generator should drop support for Related: I'm having trouble with |
Externs used in Elemental2 should not use closure enum as they represent native api. If your extern represents a closure library, it can use closure enum. |
Stupid question: If it is a closure library, then by definition it isn't an extern, right? Wouldn't it just be the closure library as input at that point? Assuming so, could the jsinterop-generator tool detect the presence/absence of Concrete example of "closure-compiler provided externs, which definitely do not refer to a closure-library": https://github.com/google/closure-compiler/blob/master/contrib/externs/maps/google_maps_api_v3.js#L4713-L4728 |
A closure library can provide an extern file if it does not provide the src but a binary (compiled js). The maps api is the concrete example. |
Can you clarify the distinction then you were making, where "externs used in elemental2 should not use closure enum"? I'm afraid I'm not understanding the point you were trying to convey. |
Elemental2 is the jsinterop version of apis provided by the browser. Browser code is not closure annotated code and they never define a closure type enum. Google maps is not an api provided by a browser but by a third party javascript library. As this library is written using closure type system, it can define closure enum type. |
I don't think Google Maps vs Browser change things much. |
@jDramaix right - I'm filing this issue against jsinterop-generator ("Generates Java annotated with JsInterop from JavaScript extern sources"), not elemental2. My only goal in this is to understand/document/expand the limitations of jsinterop-generator when trying to generate jsinterop sources based on arbitrary externs, not to specifically tailor this work to browser apis. |
So what is the resolution here? Goktug indicated that a change could be made on closure, but I don't quite follow what that change would be:
Would this mean that in generating externs, you would get And for projects which have closure JS side by side with Java (see #44), is it expected then that |
I'm not following most of your questions/conclusions. You can just send a patch to closure extern to make it a number. |
@gkdn ok, that was one of the options I was trying to offer, sorry if that was not clear. Are you saying that generally when we find an |
Yes, that's what we were told earlier. |
Thanks, I appreciate the clarification. |
Steps to repro - use an externs file with an enum that is referenced by a union type. Roughly:
The error is when j2cl tries to transpile the sources into js, it is reported that the union type is attempting an
instanceof
on the enum instead of just treating it like a number (which in this case is all it is).Easy workaround: rewrite ObjectWithProperties.thing's type to be {number|string}, though that defeats the purpose a bit.
Proposed fix (not having given it great thought): The union type should instanceof based on the type label: in this case,
number
. This seems to be a bit difficult to implement though, since the original@enum
might be already made into a java type? Thejsinterop.generator.model.Type
instances don't keep track of the type label at this time, so that would need to be added? It could potentially also be helpful in general, j2cl itself could implement$isInstance
with this knowledge if it were part of the@JsEnum
annotation?Easier error reporting option: instead of waiting for j2cl to fail the generated java, something like this could throw when the generator is trying to emit the union's isFoo() method:
The text was updated successfully, but these errors were encountered: