tags:

views:

154

answers:

2

I'm trying to use the java jcommander library from Scala. The java JCommander class has multiple constructors:

 public JCommander(Object object)  
 public JCommander(Object object, ResourceBundle bundle, String... args)   
 public JCommander(Object object, String... args)   

I want to to call the first constructor that takes no varargs. I tried:

jCommander = new JCommander(cmdLineArgs)

I get the error:

error: ambiguous reference to overloaded definition,
both constructor JCommander in class JCommander of type (x$1: Any,x$2: <repeated...>[java.lang.String])com.beust.jcommander.JCommander
and  constructor JCommander in class JCommander of type (x$1: Any)com.beust.jcommander.JCommander
match argument types (com.lasic.CommandLineArgs) and expected result type com.beust.jcommander.JCommander
jCommander = new JCommander(cmdLineArgs)

I've also tried using a named parameter, but got the same result:

jCommander = new JCommander(`object` = cmdLineArgs)

How do I tell Scala I want to call the constructor that doesn't take varargs?

I'm using Scala 2.8.0.

+5  A: 

Sorry, I now notice this is a known interoperability problem with Java. See this question and the ticket. The only work around I know of is to create a small Java class just to disambiguate these calls.

Daniel
Thanks for the information Daniel.
Brian Pugh
A: 

I think your easiest option is to have a Java class with a factory method to bridge the issue:

package com.beust.jcommander;

public class JCommanderFactory {
    public static createWithArgs(Object cmdLineArgs) {
        return new JCommander(cmdLineArgs);
    }
}

Alternatively you could use http://jewelcli.sourceforge.net/usage.html instead. JewelCli has an unambiguous factory method for the same purpose and also uses PICA (Proxied Interfaces Configured with Annotations) technique http://www.devx.com/Java/Article/42492/1954.

In fact I have an example of using JewelCLI with Scala here on Stack Overflow.

Alain O'Dea