Wednesday, August 16, 2017

Re: Dealing with javascript functions as parameters and properties



Am Mittwoch, 16. August 2017 09:24:49 UTC+2 schrieb Bill Tang:
I am working on a Jsinterop wrapper for the GSAP javascript library, and I am quite new to GWT, so I have come across a few problems I am not sure how to solve. 

My main issue revolves around trying to wrap javascript functions into Java objects. 

For example, if I have a native javascript function that has a function as one of its parameters, how can I go about wrapping this function
in java? 

The parameter that is a JS Function should be modeled using a Java functional interface, e.g. JsFunction<In, Out> { Out exec(In input); }. Then you can use Java method references or lambdas.

 
And if a javascript class has a function has one of its properties, how can I go about wrapping this property?

Similar as above. In Java the property type would be a @JsFunction annotated interface.
 

I've had some success using the @JsFunction annotation along with functional interfaces, but I am faced with the problem
where I have to declare return types, parameters, and paramater types in the java interface, when I want to be able to supply
any arbitrary function, just like I would in native javascript. Is there any way to mimic this functionality? 

Java is strongly typed, so some JS things are not possible in Java. Do you have a concrete example? Usually JS libs have some rules to functions and which parameters they expect. If you have trouble with so called "union types" you can take a look at https://docs.google.com/document/d/14mQeAGQ9M_5uTTUbzRQzCYETA887dTO-xFLtQhgUXXk/edit to see how the jsinterop generator handles it.

-- J.

--
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 https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment