-
Notifications
You must be signed in to change notification settings - Fork 146
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
Enum.valueOf() support? #90
Comments
The main issue is not the ability to implement it but rather how to implement it without losing optimization. As with other reflective features, if not done very carefully the result in a big loss of optimizations. In this case it, a naive implementation will result in all enum values for all enums being kept and not optimized away. To implement this properly might require some restrictions and compiler transformations to ensure optimizability. |
Thanks Roberto. Can you outline briefly what your acceptance criteria for a feature like this might look like? Keeping in mind of course that some parts of //transpiler/javatests can't be built in open source (com/google/j2cl/transpiler/readable and com/google/j2cl/transpiler/regression), plus all of //jre/javatests. At least in my head, it seems plausible that this could be as simple as adding a simple test to |
Without restrictions (or a specific global optimizations in jscompiler) a single use of an utility method like
would retain all unreferenced values on all enums. As I before said implementing the functionality is trivial and is not blocking this API. We need to decide on a set of restrictions and/or specific jscompiler optimizations before implementing it. We have already had some discussions around this issue but no concrete design nor plan yet. |
To confirm, you are saying that if This has come up a few times in open source, so it might be something we're going to pursue, even if we just maintain it in our fork after confirming that some basic usage like I described works. |
It is complicated to tackle this in JsCompiler. Our plan is to support this only for limited cases and do some rewriting on J2CL. If we take an example use case: First So it means that it could only be called like: If J2CL sees such definition, it will allow the call for Then J2CL will re-write such methods to look like following instead: Then it will replace And finally the call site will be re-written to look like following: |
Got it, thank you. I imagine this will be an internal-only annotation, akin to the other javaemul-internal annotations? I get the impression you aren't looking for an external contribution for this - is that accurate, or am I misreading? |
The annotation will be public so Guava and user utilities could use it as well. We will be happy to have an external contribution. If you are planning to do, I recommend writing a short design doc first to get early feedback. |
Can i make a suggestion that along with an |
If others stumble over this, here is the best current workaround I found. Given an enum like
and this helper
Usage is
|
Consider instead just using J2CL's tests confirm that this method will be emitted, and will work as expected: j2cl/transpiler/javatests/com/google/j2cl/readable/java/enums/output_closure/Enum1.impl.java.js.txt Lines 30 to 37 in 20e8f01
|
Enum.valueOf()
(and the correspondingClass.getEnumConstants()
) isn't supported in j2cl, but there are TODOs in the code suggesting that this might be feasible, and perhaps would be accepted as an external contribution?j2cl/jre/java/java/lang/Enum.java
Line 56 in d555a33
j2cl/jre/java/java/lang/Class.java
Lines 100 to 103 in d555a33
Having not yet tried, my hunch was that this might have been left out due to its interaction with @JsEnum
j2cl/transpiler/java/com/google/j2cl/generator/JavaScriptImplGenerator.java
Lines 597 to 601 in d555a33
SomeEnum.values()
(viacom.google.j2cl.frontend.common.EnumMethodsCreator#createValuesMethod
) could possibly be passed made into an additional argument forUtil.$setClassMetadataForEnum
so it is available on the Class itself?The text was updated successfully, but these errors were encountered: