Sunday, November 8, 2015

GWT 2.8 JsInterop, arrays and casts

Hello,

I tried the following code:

@JsType(namespace=JsPackage.GLOBAL, isNative=true)
public class Array {
    public native int push(int element);
    public native int push(int element, int element2);
    public native int push(String element);
    public native int pop();
}

private native int[] initTest() /*-{
    return [1,2,3,4];
}-*/;
private native Array castArray(int[] arr) /*-{
    return arr;
}-*/;

public void onModuleLoad() {
    int[] ar = initTest();
    Array jsArray = castArray(ar); // Do I really need a second variable here ?
    jsArray.push(10);              // Works
    jsArray.push(15, 16);          // Overload works !!
    jsArray.push("abc");           // Works ??
    int x1 = jsArray.pop();        // Works (x1 has value "abc") ???
    int x2 = jsArray.pop();        // Works
    int x3 = ar[4];                // Works (x3 == 10)
    int x = x1 + x2 + x3;          // Works (x = "abc1610")
    
...

a. I wonder if there is a simpler method of treating JS arrays without the need of having two variables pointing to them. I tried to use an overlay method in Array class but without any luck.

b. Although lack of checks in castings is often convenient while working with JsInterop, it could be helpful if there was a separate type/class (e.g. JsValue) accepting values of any JS type (including primitives) and providing casting methods to the other types with debugging time checks (asserts). This way a better balance between flexibility and strong casting could be achieved.

Regards,
Elias

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment