score:1
You can call the constructor with varags, but pass an empty list of varags.
(Of course, if you know that constructing JCommander with empty varags will produce the same result as calling the overloaded constructor (or method) without vargs)
jCommander = new JCommander(cmdLineArgs, Nil: _*)
score:2
The way to avoid this ambiguity is to force the compiler to pick the overload that takes more than one argument, using Scala's collection explosion syntax to pass in a singleton collection:
import java.util.stream.Stream
val stream = Stream.of(List(1):_*)
score:5
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.
score:11
The only Scala solution to this problem that I know involves reflection.
Ambiguous Methods
Let's suppose we have a Java test class:
public class Ambig {
public Ambig() {}
public String say(Object o) { return o.toString(); }
public String say(Object o, String... ss) { return o.toString()+ss.length; }
}
We can get access to the method via reflection directly:
val ambig = new Ambig
val methods = ambig.getClass.getMethods.filter(_.getName == "say")
val wanted = methods.find(_.getParameterTypes.length == 1).get
wanted.invoke(ambig, Some(5)).asInstanceOf[String]
or we can use structural types (which use reflection under the hood) to achieve the same thing with less boilerplate:
def sayer(speaker: { def say(o: Object): String }, o: Object) = speaker.say(o)
sayer(new Ambig, Some(5))
Ambiguous Constructors
Our strategy has to differ because we don't actually have an object to begin with. Let's suppose we have the Java class
public class Ambig2 {
public final String say;
public Ambig2(Object o) { say = o.toString(); }
public Ambig2(Object o, String... ss) { say = o.toString()+ss.length; }
}
The structural types approach no longer works, but we can still use reflection:
val mkAmbig2 = classOf[Ambig2].getConstructors.filter(_.getParameterTypes.length==1)
val ambig = mkAmbig2.head.newInstance(Some(5)).asInstanceOf[Ambig2]
ambig.say // Some(5)
score:18
Sorry, I now realize 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.
Source: stackoverflow.com
Related Query
- How do I disambiguate in Scala between methods with vararg and without
- Eta-expansion between methods and functions with overloaded methods in Scala
- How to find intersection between a specific node and its neighbors in Spark GraphX with Scala
- Whats the difference between main and secondary methods in scala and how to execute mulitple functions in the scala class?
- Scala - How to create a combine ArrayList of functions with and without parameters
- Difference between function with parentheses and without
- How to use IntelliJ with Play Framework and Scala
- How to get logging working in scala unit tests with testng, slf4s, and logback
- How does Scala know the difference between "def foo" and "def foo()"?
- In scala multiple inheritance, how to resolve conflicting methods with same signature but different return type?
- How to stub a method call with an implicit matcher in Mockito and Scala
- How do I get Intellij IDEA 12.0 to work with Play Framework 2.1.0 app and Scala 2.10.0?
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- How can I syntax check a Scala script without executing the script and generating any class files?
- How does Scala distinguish between () => T and => T
- How to know if a Scala file modified with IntelliJ Idea is saved and if it is checked into CVS?
- How JVM distinguish between Scala bytecode and Java bytecode?
- Scala object private scope with inner classes and methods
- Difference between conversion with implicit function and implicit class in Scala
- How do you run cucumber with Scala 2.11 and sbt 0.13?
- How to obfuscate fat Scala Jar with Proguard and SBT
- Play: How to remove the fields without value from JSON and create a new JSON with them
- How can I add scala actors to an existing program without interfering with the normal termination behavior?
- How to use play-plugins-mailer with Play 2.3 and Scala 2.11?
- How to implement Java interface in Scala with multiple variable parameter methods (type eraser issue)?
- In Scala invoking no-parameter function with and without brackets is executed in different way
- Scala inconsistence behavior for final vals with and without type ascription
- How to define sbt plugin task within prefix and without conflicts with global scope?
- How to generate Scala setters and getters with IntelliJ IDEA
- Scala: How to invoke method with type parameter and manifest without knowing the type at compile time?
More Query from same tag
- Avoiding deprecation warning on matchPattern against a Regex in ScalaTest
- How to correctly use Akka's Event Stream?
- Structured Type to match Class Constructor
- How to use ScalaMock to evaluate that function was called with certain Spark Dataframe parameter and have useful output
- How to extract values from key value map?
- scala code special syntax
- How to map js.UndefOr[T] <–> Option[T] with js.native in Scala.js?
- Universal/generic boxing from Any to AnyRef
- Convert Scala Dataframe to HashMap
- Check if a Scala / Akka actor is terminated
- Automatic Retry on unavailable hosts (NoHostAvailableException) in Phantom DSL and Play! 2
- Scala Slick - Insert in table with omitting some columns and returning primary key of new line
- scala implicit extracted values in pattern matching?
- How to form a tuple from a tuple of tuple using scala?
- Scala parser combinators and Reader infinite loop
- How to convert a wide dataframe into a vertical dataframe in Spark Scala
- How to avoid overriding a package object in Scala?
- FileWatcher on a directory
- How to split a String by "-" only when the following character is a digit and the previous one is a letter? Java/Scala
- How to cache function result in scala
- Scala: Indexed Seq instead of List in for loop
- Primary constructor parameter declared using val allows to change the value
- Reading multiple files and extracting 1st column using scala
- Scala/Java enumerations
- Concatenating several values where the type has a Show instance
- How to avoid casts with generalized type constraint?
- how to match a spark RDD with results from JdbcRDD
- Spark SQL exception handling
- JVM: most simple way to alter code of a dependency library?
- Working with Akka streams, how can I group by strings that contain different values?