[英]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 的限制吗?
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:对于
A
和B
与T
同时被编译器绑定的简单情况, 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:当
A
和B
已经像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
的子类型,而不是A
是A1
和T
的子类型。)
A1
and B1
are here for the sole purpose of inferring a correct type for T
. A1
和B1
在这里的唯一目的是推断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 = A
和B1 = B
,然后T
作为最具体的类型,它是A
和B
的超类。
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 >: A
和T >: B
,这直接来自关于A1
和B1
的约束。 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
不能使用这种技术,因为它是在我们甚至不知道A
和B
的地方定义的,或者实际上在具体子类中有多少类型参数。
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.