17

Possible Duplicate:
How do I disambiguate in Scala between methods with vararg and without

I am currently porting part of an application to scala and it uses the Oval library. The method is question is the Validator.validate method. It has two signatures:

List<ConstraintViolation> validate(Object validatedObject)
List<ConstraintViolation> validate(Object validatedObject, String... profiles) 

The scala code looks generally like this:

def validate(toValidate: AnyRef) = {
  val validator = createValidator
  validator.validate(toValidate)
}

And the error message:

error: ambiguous reference to overloaded definition,
[INFO] both method validate in class Validator of type (x$1: Any,x$2: <repeated...>[java.lang.String])java.util.List[net.sf.oval.ConstraintViolation]
[INFO] and  method validate in class Validator of type (x$1: Any)java.util.List[net.sf.oval.ConstraintViolation]
[INFO] match argument types (AnyRef)
[INFO]       this.validator.validate(toValidate)

How can I get this be be unambiguous?

Community
  • 1
  • 1
Travis Stevens
  • 2,198
  • 2
  • 17
  • 25
  • It does appear to be an ambiguous reference to an overloaded definition question on stack overflow. Should I delete this post? Although Rex Kerr did give an answer that wasn't on the original. – Travis Stevens Jun 02 '11 at 16:16
  • I recreated my answer there, and expanded it in two ways: a short form for what I wrote for methods, and an alternate that works for constructors. – Rex Kerr Jun 02 '11 at 18:10

2 Answers2

12

I think that this could be a duplicate of How do I disambiguate in Scala between methods with vararg and without

Basically, it is a known java-scala-interop problem, and the only workarounds involve extra Java adapters to make accessible in Scala.

Community
  • 1
  • 1
jkl
  • 740
  • 5
  • 10
  • Sometimes you can avoid the Java adapter by explicitly creating a `Seq` and using the [`_*` type annotation](https://stackoverflow.com/a/6051356/14379), eg `validator.validate(toValidate, Array[String](): _*)`. See answers by Yawar and Teimuraz in the [duplicate question](https://stackoverflow.com/q/3313929/14379). – seanf Jul 01 '19 at 06:32
5

The only way I know of is to use reflection:

val ambiguous = validator.getClass.getMethods.filter(_.getName == "validate")
val wanted = ambiguous.find(_.getParameterTypes.length == 1).get
wanted.invoke(validator, toValidate).asInstanceOf[java.util.List[ConstraintViolation]]
zellus
  • 9,617
  • 5
  • 39
  • 56
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407