Java 函数带有“...”在 Scala 的参数列表中
如所述 这里
如果你想让代码编译,你必须将 :_*
关键字添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此处不得使用
:_*
。_*
存在的原因是可变参数可能会导致歧义。在java中如果你有
f(int...values)
你可以调用f(1, 2, 3)
,但也有 do与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 给出了正确的答案:
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 callf(1, 2, 3)
, but also have doWith ints, it is ok. But if you have
f(object.. values)
and haveobject[] values = {"a", "b"}
, when callingf(values)
, should it means that f is called with a single arg, which is thevalues
, 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:
对于 Scala 2.9,
cq.where(condition)
应该可以工作,甚至cq.where(condition1, condition2)
也应该可以工作。或者,您可以使用
cq.where(Array(condition):_*)
。With Scala 2.9
cq.where(condition)
should work, evencq.where(condition1, condition2)
should work.Alternatively you could use
cq.where(Array(condition):_*)
.我正在使用 scala 库 2.9.0.1。这是该代码的编译器错误,仅传递条件:
问题出在哪里?
如果我使用
我会收到相同的编译器错误:
也许我使用的 scala 编译器?
亲切的问候
马西莫
I'm using scala library 2.9.0.1. This is the compiler error for that code, only passing the condition:
Where is the problem?
If I use
I receive the same compiler error:
Maybe the scala compiler that I use?
KInd regards
Massimo