Scala - 对重载定义的不明确引用 - 带可变参数

发布于 2024-11-13 17:24:29 字数 1395 浏览 2 评论 0原文

可能的重复:
如何在 Scala 中消除方法之间的歧义有 vararg 和没有

我目前正在将应用程序的一部分移植到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心 2024-11-20 17:24:29

我认为这可能是 如何在 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.

悸初 2024-11-20 17:24:29

我知道的唯一方法是使用反射:

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]]

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]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文