Wolkenarchitekt
Wolkenarchitekt

Reputation: 21238

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)

Upvotes: 5

Views: 400

Answers (1)

RoToRa
RoToRa

Reputation: 38410

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

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

Upvotes: 9

Related Questions