Search code examples
javascriptjsongwtoverlayjsni

Why doesn't JsArrayString implement iterable?


Quick question here -- I just used JsArrayString for the first time, and was surprised that this didn't work:

JsArrayString object = ...;
for (String s : object)

So I just wrote a C-style for loop:

JsArrayString object = ...;
for (int i=0; i<object.length(); i++) {
    s = object.get(i)
    ...

Not a big deal, but seems like it would have been simple for the GWT team to have JSArrayString implement iterable, so I wanted to check and make sure I wasn't missing something...


Solution

  • I suspect it's a matter of code bloat and for that matter code size. If they make JsArray implement Iterable, it might open the door for other additions that wouldn't always be useful. JsArrays are meant to be absolutely as simple and barebones as possible.

    Additionally, you could write your own JsIterable class that does this if you want this behavior, as you said it should be pretty trivial to implement.

    The Lightweight Collections design doc addresses some of the issues around using JRE collections and related concepts and discusses which features could be left unsupported to ensure absolute minimum code size, including:

    Until the GWT compiler can optimize it (which it cannot to date), the new collections will not support the Java enhanced 'for' loop syntax that uses Iterable/Iterator. We do believe such optimizations are possible and will be added, however, at which time, these collections will be retrofitted to implement Iterable.