Quantcast

Use Java function with "..." in parameter list from Scala

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Use Java function with "..." in parameter list from Scala

dubbyconqueror
I'm trying to use the following Java function from Scala 2.8RC3:
(it's from JPA 2.0 criteria query, javax.persistence.criteria.CriteriaQuery)

    CriteriaQuery<T> where(Predicate... restrictions);

In Java, i would use the function like this (the only important line is the last one):  
    CriteriaQuery<Person> cq = queryBuilder.createQuery(Person.class);
    Root<Person> person = cq.from(Person.class);
    Predicate condition = queryBuilder.equal(person.get(Person_.email), p.getEmail());
    cq.where(condition);   
   
To use the function in Scala, i wrote the following code:
     val cq = queryBuilder.createQuery(classOf[Person])
     val person:Root[Person] = cq.from(classOf[Person])
     val condition:Predicate = queryBuilder.equal(person.get(Person_.email), p.getEmail)
     cq.where(condition)

But this results in the following compile time exception:
    ambiguous reference to overloaded definition, both method where in trait CriteriaQuery of type (x$1: <repeated...>
    [javax.persistence.criteria.Predicate])javax.persistence.criteria.CriteriaQuery[model.Person] and method where in trait
    CriteriaQuery of type (x$1: javax.persistence.criteria.Expression[java.lang.Boolean])javax.persistence.criteria.CriteriaQuery[model.Person] match argument
    types (javax.persistence.criteria.Predicate)

I already tried things like:
    cq.where(Array(condition))

But that results in
    overloaded method value where with alternatives: (<repeated...>[javax.persistence.criteria.Predicate])javax.persistence.criteria.CriteriaQuery[model.Person] <and>
    (javax.persistence.criteria.Expression[java.lang.Boolean])
    javax.persistence.criteria.CriteriaQuery[model.Person] cannot be applied to (Array[javax.persistence.criteria.Predicate])

How can i solve this problem and achieve the same effect in my Scala snippet as in Java?

Thanks in advance!!!
Ingo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use Java function with "..." in parameter list from Scala

Stefan Langer-4
Have you tried
cq.where(condition: _*) ?

-Stefan

2010/6/11 Ingo Fischer <[hidden email]>:

> I'm trying to use the following Java function from Scala 2.8RC3:
> (it's from JPA 2.0 criteria query, javax.persistence.criteria.CriteriaQuery)
>
>     CriteriaQuery<T> where(Predicate... restrictions);
>
> In Java, i would use the function like this (the only important line is the
> last one):
>     CriteriaQuery<Person> cq = queryBuilder.createQuery(Person.class);
>     Root<Person> person = cq.from(Person.class);
>     Predicate condition = queryBuilder.equal(person.get(Person_.email),
> p.getEmail());
>     cq.where(condition);
>
> To use the function in Scala, i wrote the following code:
>      val cq = queryBuilder.createQuery(classOf[Person])
>      val person:Root[Person] = cq.from(classOf[Person])
>      val condition:Predicate = queryBuilder.equal(person.get(Person_.email),
> p.getEmail)
>      cq.where(condition)
>
> But this results in the following compile time exception:
>     ambiguous reference to overloaded definition, both method where in trait
> CriteriaQuery of type (x$1: <repeated...>
>
> [javax.persistence.criteria.Predicate])javax.persistence.criteria.CriteriaQuery[model.Person]
> and method where in trait
>     CriteriaQuery of type (x$1:
> javax.persistence.criteria.Expression[java.lang.Boolean])javax.persistence.criteria.CriteriaQuery[model.Person]
> match argument
>     types (javax.persistence.criteria.Predicate)
>
> I already tried things like:
>     cq.where(Array(condition))
>
> But that results in
>     overloaded method value where with alternatives:
> (<repeated...>[javax.persistence.criteria.Predicate])javax.persistence.criteria.CriteriaQuery[model.Person]
> <and>
>     (javax.persistence.criteria.Expression[java.lang.Boolean])
>     javax.persistence.criteria.CriteriaQuery[model.Person] cannot be applied
> to (Array[javax.persistence.criteria.Predicate])
>
> How can i solve this problem and achieve the same effect in my Scala snippet
> as in Java?
>
> Thanks in advance!!!
> Ingo
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use Java function with "..." in parameter list from Scala

dubbyconqueror
In reply to this post by dubbyconqueror
Just to be complete, here are the two functions (i forgot to mention
both in my first mail) which are ambiguous in Scala and not in Java

CriteriaQuery<T> where(Predicate... restrictions);
CriteriaQuery<T> where(Expression<Boolean> restriction);


Am 11.06.2010 09:28, schrieb Francois Armand:

> Le 11/06/2010 02:27, Ingo Fischer a écrit :
>    
>> How can i solve this problem and achieve the same effect in my Scala
>> snippet as in Java?
>>
>>      
> If you want to pass an array or a collection as a repeated argument, you
> have to annotated it with :_*
>
> So, you could try:
>
> cq.where(Array(condition):_*)
>    

ugly, but it actually works, thanks!

>
> That seems to come really often in the mailing list...
>
>
> For the problem you have with direct call (condition not wrapped in an
> array), I don't know why the two methods are not ambiguous in Java.
> (I suppose that Predicate is a subclass of
> javax.persistence.criteria.Expression[java.lang.Boolean] ?)
>    
That's true, Predicate is actually a subclass of
"javax.persistence.criteria.Expression[java.lang.Boolean]"

> Perhaps somebody more type resolution aware could teach us ?
>
>    
Yea i also would like to know that...


Rob
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use Java function with "..." in parameter list from Scala

Rob
This post has NOT been accepted by the mailing list yet.
This post was updated on .
In reply to this post by Stefan Langer-4
Stefan Langer-4, you are my hero. ;-)
Loading...