@FunctionalInterface VS @JsFunction

56 views
Skip to first unread message

Jan Blok

unread,
Jan 15, 2018, 10:03:11 AM1/15/18
to GWT Users
Hi,

Would it be possible for GWTc to handle java.lang.FunctionalInterface as if it was jsinterop.annotations.JsFunction ?
The reason for asking is, I have this java code:

    public void transaction(Runnable runnable) {
        startTransaction();
        try {
            runnable.run();
        }
        finally {
            endTransaction();
        }
    }

when I expose this method via jsinterop I get a warning "not usable from javascript"

I intent to use it from JS/TS as: myJavaLib.transaction(() => doSomethingInTransaction(...));

but this is not possible unless I supersource java.lang.Runnable and add @JsFunction 

It seems to me that @FunctionalInterface and @JsFunction are very similar already...

Regards Jan 


Thomas Broyer

unread,
Jan 15, 2018, 10:11:45 AM1/15/18
to GWT Users


On Monday, January 15, 2018 at 4:03:11 PM UTC+1, Jan Blok wrote:
Hi,

Would it be possible for GWTc to handle java.lang.FunctionalInterface as if it was jsinterop.annotations.JsFunction ?

No, if only because JsFunction has many constraints: http://www.gwtproject.org/javadoc/latest/jsinterop/annotations/JsFunction.html
 
The reason for asking is, I have this java code:

    public void transaction(Runnable runnable) {
        startTransaction();
        try {
            runnable.run();
        }
        finally {
            endTransaction();
        }
    }

when I expose this method via jsinterop I get a warning "not usable from javascript"

I intent to use it from JS/TS as: myJavaLib.transaction(() => doSomethingInTransaction(...));

but this is not possible unless I supersource java.lang.Runnable and add @JsFunction 

You'd need to create an overload taking a JsFunction as input, and exposing that one.

@JsFunction @FunctionalInterface
interface TransactionCallback {
 
void run();
}


@JsMethod
public void transaction(TransactionCallback callback) {
  startTransaction
();
 
try {
    callback
.run();
 
} finally {
    endTransaction
();
 
}
}

// Note: not a @JsMethod
public void transaction(Runnable runnable) {
  transaction
((TransactionCallback) runnable::run);
}

 
It seems to me that @FunctionalInterface and @JsFunction are very similar already...

And this is why the javadoc for JsFunction recommends also annotating the interface with FunctionalInterface. 

Jan Blok

unread,
Jan 15, 2018, 11:23:39 AM1/15/18
to GWT Users
Yeah sure, there are many workarounds...which leads to bloat as shown.
Then my request would be to by default have Runnable and Callable in gwt java emulation to be attributed by default with interop.JsFunction :-)

Regards Jan

Thomas Broyer

unread,
Jan 15, 2018, 11:40:59 AM1/15/18
to GWT Users
Which is not possible due to the many contraints of JsFunction, that would break a whole lot of apps.
Reply all
Reply to author
Forward
0 new messages