简体   繁体   English

Scala 中的多个下限

[英]Multiple lower type bounds in Scala

I noticed that tuple.productIterator always returns an Iterator[Any] an wondered if it's not possible to set multiple lower bounds (so it could be an Iterator of the lowest common super type).我注意到tuple.productIterator总是返回一个Iterator[Any]并想知道是否不能设置多个下限(因此它可能是最低通用超类型的 Iterator)。

I tried and searched a bit, but only found this question for multiple upper bounds.我尝试并搜索了一下,但只找到了多个上限的这个问题。

This is my test on how to define the the type of the iterator:这是我对如何定义迭代器类型的测试:

def f[A,B](a:A, b:B) = List(a,b)
// return type is List[Any]

def f[A,B, T >: A "and" T >: B](a:A, b:B) = List[T](a,b)
// doesn't compile, but
//  f(1, true) should give a List[AnyVal] and
//  f("x", "y") a List[String]

Is this a limitation of the JVM?这是 JVM 的限制吗?


Edit: Here's a slightly bigger example which doesn't seem to be solvable using IttayD approach when T should be defined in the method: 编辑:这是一个稍微大一点的例子,当 T 应该在方法中定义时,使用 IttayD 方法似乎无法解决:

 class Foo[A, B](a: A, b: B) { def f[T >: A] = List[T](a) // works def g[T >: A "and" T >: B] = List[T](a) // doesn't work }

For the simple case where A and B are bound by the compiler as the same time as T , IttayD's answer works fine:对于ABT同时被编译器绑定的简单情况, IttayD 的答案可以正常工作:

def f[T, A <: T,B <: T](a:A, b:B) = List[T](a,b)

When A and B are already bound as in your class Foo[A, B] example, you need to introduce temporary dummy variables to have the compiler do this job:AB已经像class Foo[A, B]示例中那样绑定时,您需要引入临时虚拟变量以让编译器完成这项工作:

class Foo[A, B](a: A, b: B) {
  def g[T, A1 >: A <: T, B1 >: B <: T] = List[T](a: A1, b: B1)
}

(For the sake of clarity: A1 >: A <: T means that type A1 must be a supertype of A and a subtype of T , and not that A is a subtype of both A1 and T .) (为了清楚起见: A1 >: A <: T表示类型A1必须是A的超类型和T的子类型,而不是AA1T的子类型。)

A1 and B1 are here for the sole purpose of inferring a correct type for T . A1B1在这里的唯一目的是推断T的正确类型。 If the compiler has to infer them, they will resolve to A1 = A and B1 = B , and then T as the most specific type which is a superclass of both A and B .如果编译器必须推断它们,它们将解析为A1 = AB1 = B ,然后T作为最具体的类型,它是AB的超类。

One thing that the compiler doesn't realize, though, is that, by transitivity, we have both T >: A and T >: B , which follows directly from the constraints with respect to A1 and B1 .但是,编译器没有意识到的一件事是,通过传递性,我们同时拥有T >: AT >: B ,这直接来自关于A1B1的约束。 We need to help out with the type ascriptions.我们需要帮助解决类型归属问题。

Now, Product#productIterator could not use this technique, as it's defined in a place where we don't even know A and B , or indeed how many type parameters there are in the concrete subclass.现在, Product#productIterator不能使用这种技术,因为它是在我们甚至不知道AB的地方定义的,或者实际上在具体子类中有多少类型参数。

It sounds like what you need is an HList: http://apocalisp.wordpress.com/2010/07/06/type-level-programming-in-scala-part-6a-heterogeneous-list%C2%A0basics/听起来您需要的是一个 HList: http://apocalisp.wordpress.com/2010/07/06/type-level-programming-in-scala-part-6a-heterogeneous-list%C2%A0basics/

To answer the specific question:要回答具体问题:

scala> def f[T, A <: T,B <: T](a:A, b:B) = List[T](a,b)
f: [T, A <: T, B <: T](a: A, b: B)List[T]

scala> f(1, true)
res0: List[AnyVal] = List(1, true)

scala> f("x", "y")
res1: List[java.lang.String] = List(x, y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM