elemental2: JsArray.slice() always causes ClassCastException

Apologies if this should not be a bug (or should be filed against GWT itself), but it was a surprise to discover (and a pain to debug) - JsArray<T>.slice() returns T[], which always causes a class cast exception, at least where T is java.lang.String. Workaround: use JsString or Object.

Steps to reproduce:

    JsArray<String> strings = new JsArray<>("a", "b", "c");
    String[] sliced = strings.slice();
    DomGlobal.window.alert(sliced[0]);

Replace String with either JsString or Object above, and it will work. Note that Double fails in the same way, despite ostensibly also being a value that can be passed interchangeably in and out of other JS methods. In fact, it appears that even elemental2 expects that this will work correctly: elemental2.core.ITemplateArray apparently extends JsArray<String>.

I’m not sure how this issue can be reasonably mitigated, aside from baking checks into GWT that this sort of thing isn’t safe. Perhaps those methods should be annotated with @UncheckedCast?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (13 by maintainers)

Commits related to this issue

Most upvoted comments

At a minimum I think JsArray<T> methods should return JsArray<T> (methods on other classes could continue to return arrays); there might be other classes/methods with similar cases (maybe all “generic arrays” should be JsArrays?)