Scala - 对重载定义的不明确引用 - 带可变参数
我目前正在将应用程序的一部分移植到 scala,它使用 Oval 库。有问题的方法是 Validator.validate 方法。它有两个 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)
如何才能做到这一点是明确的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这可能是 如何在 Scala 中消除带 vararg 和不带 vararg 的方法之间的歧义
基本上,这是一个已知的 java-scala-interop 问题,唯一的解决方法涉及额外的 Java 适配器,以便在 Scala 中进行访问。
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.
我知道的唯一方法是使用反射:
The only way I know of is to use reflection: