google / j2cl

Java to Closure JavaScript transpiler
Apache License 2.0
1.23k stars 144 forks source link

Symbol fields #125

Closed rendaw closed 3 years ago

rendaw commented 3 years ago

Intl.Segmenter.segment() appears to return an object that only has a [Symbol.iterator] property for accessing the results. Is there any way to access this from Java? get in PropertyMap takes a string, and I can't bind it with @JsMethod since the symbol can't be used as a name there.

I'm going to try to make a helper JS function to do it, but I was wondering if there's a canonical way to do it. I searched the js interop guides, Nextgen doc, issues, etc. and I don't think I saw this mentioned.

gkdn commented 3 years ago

Currently Jsinterop doesn't directly support Symbols.

Being said that [Symbol.iterator] wouldn't be terrible helpful in Java side even if it did exist. That is essentially used as part of the contract of for..of statements or other builtin functions that takes iterables and you can still use them in those contexts.

For example; you can write:

JsArray segments = JsArray.from(Intl.Segmenter.segment());

and then use the JsArray APIs.

Also if you like, you can send a patch to https://github.com/google/jsinterop-base to add JsPropertyMap.forOfEach that will work similarly to the existing JsPropertyMap.forEach functionality.

rendaw commented 3 years ago

Okay, thanks! I'll look into that.