可能重复:
How do I disambiguate in Scala between methods with vararg and without
我目前正在将应用程序的一部分移植到scala,它使用Oval库。方法问题是Validator.validate方法。它有two signatures:
List<ConstraintViolation> validate(Object validatedObject)
List<ConstraintViolation> validate(Object validatedObject, String... profiles)
scala代码看起来像这样:
def validate(toValidate: AnyRef) = {
val validator = createValidator
validator.validate(toValidate)
}
错误信息:
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)
我怎样才能明白这一点?
答案 0 :(得分:11)
我认为这可能与How do I disambiguate in Scala between methods with vararg and without
重复基本上,它是一个已知的java-scala-interop问题,唯一的解决方法是在Scala中使用额外的Java适配器。
答案 1 :(得分:5)
我所知道的唯一方法是使用反射:
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]]