Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala method where type of second parameter equals part of generic type from first parameter

I want to create a specific generic method in Scala. It takes two parameters. The first is of the type of a generic Java Interface (it's from the JPA criteria query). It currently looks like this:

def genericFind(attribute:SingularAttribute[Person, _], value:Object) {
  ...
}

// The Java Interface which is the type of the first parameter in my find-method:
public interface SingularAttribute<X, T> extends Attribute<X, T>, Bindable<T>

Now i want to achieve the following: value is currently of type java.lang.Object. But I want to make it more specific. Value has to be the of the same type as the placeholder "_" from the first parameter (and so represents the "T" in the Java interface).

Is that somehow possible, and how?

BTW Sorry for the stupid question title (any suggestions?)

EDIT: added an addtional example which could make the problem more clear:

// A practical example how the Scala method could be called 

// Java class:
public class Person_ {
  public static volatile SingularAttribute<Person, Long> id;
}

// Calling the method from Scala:
genericFind(Person_.id, Long)
like image 646
Wolkenarchitekt Avatar asked Jun 18 '10 10:06

Wolkenarchitekt


1 Answers

Of the top of my head (I'm still starting with Scala):

def genericFind[T](attribute:SingularAttribute[Person, T], value:T) {
  ...
}
like image 189
RoToRa Avatar answered Sep 20 '22 12:09

RoToRa