代码之家  ›  专栏  ›  技术社区  ›  Wolkenarchitekt

Scala方法,其中第二个参数的类型等于第一个参数的泛型类型的一部分

  •  5
  • Wolkenarchitekt  · 技术社区  · 14 年前

    我想在Scala中创建一个特定的泛型方法。它需要两个参数。第一种是泛型Java接口(来自JPA条件查询)。现在看起来是这样的:

    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>
    

    现在我想实现以下目标: 价值

    这有可能吗?怎么可能?

    编辑: 添加了一个可以使问题更清楚的附加示例:

    // 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)
    
    1 回复  |  直到 14 年前
        1
  •  9
  •   RoToRa    14 年前

    在我的头顶(我还是从Scala开始):

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