Java 函数带有“...”在 Scala 的参数列表中

发布于 2024-12-01 11:21:51 字数 699 浏览 2 评论 0原文

如所述 这里

如果你想让代码编译,你必须将 :_* 关键字添加到 condition:Predicate

现在我有这个问题

val queryBuilder = em.getCriteriaBuilder()
     val cq = queryBuilder.createQuery(classOf[Product])
     val product:Root[Product] = cq.from(classOf[Product])
     val condition:Predicate = queryBuilder.equal(product.get("name"), "name")
     --> cq.where(condition:_*)


Multiple markers at this line
- type mismatch; found : javax.persistence.criteria.Predicate required: Seq[?]

有什么想法吗?

As described here

If you want to make the code compile you have to add the :_* keyword to the condition:Predicate

Now I have this problem

val queryBuilder = em.getCriteriaBuilder()
     val cq = queryBuilder.createQuery(classOf[Product])
     val product:Root[Product] = cq.from(classOf[Product])
     val condition:Predicate = queryBuilder.equal(product.get("name"), "name")
     --> cq.where(condition:_*)


Multiple markers at this line
- type mismatch; found : javax.persistence.criteria.Predicate required: Seq[?]

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

十六岁半 2024-12-08 11:21:51

此处不得使用 :_*

_* 存在的原因是可变参数可能会导致歧义。
在java中如果你有f(int...values)你可以调用f(1, 2, 3),但也有 do

int[] values = {1, 2, 3}; 
f(values)

与ints,这是可以的。但是,如果您有 f(object..values) 且有 object[] value = {"a", "b"},则在调用 f(values ),是否意味着使用单个参数(即 values)调用 f,或者使用多个参数 "a""b “? (java选择后者)。

为了避免这种情况,在 scala 中,当存在可变参数时,不允许将它们作为数组(实际上是 scala 中更通用的 Seq)参数传递,除非你明确表示你正在这样做,通过放置缩写 :_*。在前面的示例中,f(values) 表示值是单个参数。 f(values: _*) 表示 value 的每个元素,无论是零个、一个还是多个,都是一个参数。

在您的特定情况下,您将 Predicate 参数(实际上只是一个)作为单独的(好吧......)谓词传递,而不是作为谓词集合。所以不:_*

编辑:我更仔细地阅读了您链接到的帖子。虽然我确信我上面写的内容是正确的,但它可能没有帮助。 MxFr 给出了正确的答案:

  1. 从 scala 2.9 开始,与 java varargs 的交互是可以的。只需传递条件
  2. 在此之前,您必须将参数作为数组传递,并放入 :_*

You must not use :_* here.

There reason why _* exists is that varargs may lead to ambiguities.
In java If you have f(int... values) you may call f(1, 2, 3), but also have do

int[] values = {1, 2, 3}; 
f(values)

With ints, it is ok. But if you have f(object.. values) and have object[] values = {"a", "b"}, when calling f(values), should it means that f is called with a single arg, which is the values, or with multiple args "a" and "b"? (java chooses the later).

To avoid that, in scala, when there are varargs, it is not allowed to pass them as arrays (actually the more general Seq in scala) argument, except if you explicitly says you are doing so, by putting the abscription :_*. With the previous example, f(values) means values is the single argument. f(values: _*) means each element of values, whether there are zero, one, or many, is an argument.

In your particular case, you are passing the Predicate arguments (actually just one) as separate (well...) predicates, not as a collection of predicates. So no :_*

Edit: I read the post you linked to more carefully. While what I certainly believe what I have written above is true, it is probably not helpful. The proper answer was given by MxFr:

  1. Since scala 2.9, interaction with java varargs is ok. just pass condition
  2. Before that, you must pass your arguments as an array, and put :_*
芸娘子的小脾气 2024-12-08 11:21:51

对于 Scala 2.9,cq.where(condition) 应该可以工作,甚至 cq.where(condition1, condition2) 也应该可以工作。

或者,您可以使用cq.where(Array(condition):_*)

With Scala 2.9 cq.where(condition) should work, even cq.where(condition1, condition2) should work.

Alternatively you could use cq.where(Array(condition):_*).

笑红尘 2024-12-08 11:21:51

我正在使用 scala 库 2.9.0.1。这是该代码的编译器错误,仅传递条件:

[ERROR]     D:\Mazi\Develop\workspaces\scala\sshwWebApp\src\main\scala\com\example\app\dao\DefaultProductDao.scala:54: error: ambiguous referenc
    e to overloaded definition,
    [INFO] both method where in trait CriteriaQuery of type (x$1: <repeated...>[javax.persistence.criteria.Predicate])javax.persistence.criteria
    .CriteriaQuery[com.example.app.domain.Product]
    [INFO] and  method where in trait CriteriaQuery of type (x$1: javax.persistence.criteria.Expression[java.lang.Boolean])javax.persistence.cri
    teria.CriteriaQuery[com.example.app.domain.Product]
    [INFO] match argument types (javax.persistence.criteria.Predicate)
    [INFO]      cq.where(condition)
    [INFO]         ^

问题出在哪里?

如果我使用

cq.where(Array(condition):_*)

我会收到相同的编译器错误:

[ERROR] D:\Mazi\Develop\workspaces\scala\sshwWebApp\src\main\scala\com\example\app\dao\DefaultProductDao.scala:54: error: ambiguous referenc
e to overloaded definition,
[INFO] both method where in trait CriteriaQuery of type (x$1: <repeated...>[javax.persistence.criteria.Predicate])javax.persistence.criteria
.CriteriaQuery[com.example.app.domain.Product]
[INFO] and  method where in trait CriteriaQuery of type (x$1: javax.persistence.criteria.Expression[java.lang.Boolean])javax.persistence.cri
teria.CriteriaQuery[com.example.app.domain.Product]
[INFO] match argument types (javax.persistence.criteria.Predicate)
[INFO]      cq.where(Array(condition):_*)
[INFO]         ^

也许我使用的 scala 编译器?

亲切的问候
马西莫

I'm using scala library 2.9.0.1. This is the compiler error for that code, only passing the condition:

[ERROR]     D:\Mazi\Develop\workspaces\scala\sshwWebApp\src\main\scala\com\example\app\dao\DefaultProductDao.scala:54: error: ambiguous referenc
    e to overloaded definition,
    [INFO] both method where in trait CriteriaQuery of type (x$1: <repeated...>[javax.persistence.criteria.Predicate])javax.persistence.criteria
    .CriteriaQuery[com.example.app.domain.Product]
    [INFO] and  method where in trait CriteriaQuery of type (x$1: javax.persistence.criteria.Expression[java.lang.Boolean])javax.persistence.cri
    teria.CriteriaQuery[com.example.app.domain.Product]
    [INFO] match argument types (javax.persistence.criteria.Predicate)
    [INFO]      cq.where(condition)
    [INFO]         ^

Where is the problem?

If I use

cq.where(Array(condition):_*)

I receive the same compiler error:

[ERROR] D:\Mazi\Develop\workspaces\scala\sshwWebApp\src\main\scala\com\example\app\dao\DefaultProductDao.scala:54: error: ambiguous referenc
e to overloaded definition,
[INFO] both method where in trait CriteriaQuery of type (x$1: <repeated...>[javax.persistence.criteria.Predicate])javax.persistence.criteria
.CriteriaQuery[com.example.app.domain.Product]
[INFO] and  method where in trait CriteriaQuery of type (x$1: javax.persistence.criteria.Expression[java.lang.Boolean])javax.persistence.cri
teria.CriteriaQuery[com.example.app.domain.Product]
[INFO] match argument types (javax.persistence.criteria.Predicate)
[INFO]      cq.where(Array(condition):_*)
[INFO]         ^

Maybe the scala compiler that I use?

KInd regards
Massimo

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