Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[ease-dev] Rhino & method overloading

We had a short discussion on gerrit on method overloading in Rhino, which I would like to bring to the mailing list.

 

From my understanding Rhino supports method overloading only partially. Lets assume we hava a java class providing methods

 

foo(String);

foo(java.io.File);

foo(int);

 

now we call that Java class from our JS script using

my.JavaClass.foo(15)

In Rhino 15 is not an int type, but a variable type. Now Rhino scans for a method ‘foo’ taking a single parameter. To find the right one, it tries to convert the vararg 15 to either String, File or int. Not sure on the order it tries to convert, but if a matching method is found the parameter gets converted and the according method is called.

 

Broken down to EASE

 

foo(String);

 

@WrapToScript

foo(java.io.File);

 

would be callable using either foo(<File>) or foo(“Test”). This might be good or bad, depending how you look at things. Calling a method which was not intended to be exported might yield unexpected results to a script user, but currently I see no way to prevent this.

 

 

It gets more complicated when the java class provides

 

bar(String);

bar(int, String);

 

 

Now we have to decide whether to wrap the method with a single parameter or the method with 2 parameters. Here Rhino takes the parameter count to filter available java methods. Together with the optional parameter support from EASE I think there is no way to clearly identify which Java method to use.

 

 

Thus said, my suggestion for wrapping methods is to

·         wrap only one method per method name (do not wrap 2 methods both called ‘foo’ with different signatures)

·         use generic parameter type (eg Object) if you want to pass different parameters to your class.

·         For different parameter count use optional parameters (that is fixed parameters in Java augmented with @ScriptParameter(defaultValue = “…”))

 

 

Not sure how Python handles this topic. Will be something we have to investigate more closely to have a consistent implementation in EASE.

 

Best regards

Christian

 

Attachment: smime.p7s
Description: S/MIME cryptographic signature


Back to the top