Issue with Java varargs and Jython list

Benoit Lombardot <[email protected]> Mon, 25 Sep 2017 20:53:59 +0200
Newsgroups gmane.comp.lang.jython.user
Message-ID <[email protected]>
Dear All,

I have an issue with calling java varargs function from jython. I use 
Jython 2.7.0 from ImageJ.

My Java function expects Object... as parameter. When calling it from 
jython everything goes fine unless one of the parameter is a list. In 
that case the script complains that the Java function expects a single 
parameter but received more. I put a toy example below to illustrate. If 
anyone has idea to circumvent that issue, I'd be really grateful.

In practice I am trying to write a small library in Java to wrap some 
image analysis functions. The goal is to make the function easy to use 
for people getting started with scripting. Basically I try to avoid java 
import in the script as well as conversions or unecessary syntax that 
could be confusing to beginners. Ideally I'd like user to pass for 
instance an image and a list of number (representing the size of a voxel 
or a filter size for instance) and the jython list would be interpreted 
on the Java side as List<Float> or Float[]. Any advice to go in that 
direction would be super welcome.

Thanks!

Benoit

// java side
public class myClass{

     public void test( Object... args ){

         for(Object object : args)

             System.out.println( object.toString() );

     }

}


# jython side

import myClass

myClass().test("hello", 1 ) # this works fine

myClass().test( [ 1, 1 ] ) # this works fine too

myClass().test("hello", [ 1 , 1 ] ) # this crashes with: "TypeError: 
test(): expected 1 args; got 2"
myClass().test( [ "hello", [ 1 , 1 ] ] ) # seems to work too

PS: I've seen that putting all the parameters in a list would be 
workaround. By chance does jython 2.7.1 would allow to avoid these 
additionnal bracket?

-- 
Benoit Lombardot, PhD
Bio-Image Informatics team leader
Scientific Computing Facility

Max Planck Institute CBG
Pfotenhauer Str. 108, 01307 Dresden
room: 009 (CSBD)
phone: +49 351 210 2544

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users