Wednesday, August 16, 2017

Re: Dealing with javascript functions as parameters and properties

Hey Jens, thanks for your help!

For a concrete example, there is a javascript function in the library called delayedCall(), which takes in among its arguments, an arbitrary function, and an optional array of parameters for that 
function. Ideally, I would like to have a simple @JsMethod wrapper around this function, but I can't quite figure out how to model the function parameter, since it can be a function with arbitrary
parameter types, etc... but when using a @JsFunction annotated interface, it looks like I need to predetermine at least the types/number of parameters. Using a generic method I can use an
arbitrary parameter and return type, but I still don't have the freedom to have arbitrary number of parameters with arbitrary types. Is there any way around this, or is this just one
of those JS things not possible in Java. 



On Wednesday, August 16, 2017 at 3:17:44 AM UTC-7, Jens wrote:


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