为什么 scala Tuple productIterator 返回类型 Iterator[Any] ?
例如如果 Tuple3或 Product3 productIterator 以下定义
def productIterator[T1<:X,T2<:X,T3<:X,X] = Iterator(_1,_2,_3)
以下表达式可以返回 Iterator[java.lang.Number]
(BigInt(1),new java.lang.Long(2),new java.lang.Float(3)).productIterator
但是当前的 Scala 版本 (2.9.1) 并非如此。有什么原因吗?
最佳答案
% scala3 -Xexperimental
Welcome to Scala version 2.10.0.rdev-4056-2011-12-21-g69ed0a9 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.
scala> case class Foo[T1 <: java.lang.Number, T2 <: java.lang.Number](s: T1, t: T2)
defined class Foo
scala> Foo[java.lang.Integer, java.lang.Long](5, 5L)
res1: Foo[Integer,Long] = Foo(5,5)
scala> res1.productIterator _
res2: () => Iterator[Number] = <function0>
更新
它是乘积元素(未删除)类型的最小上限。例如在
Product[T, U, V]
它与表达式的类型相同if (cond1) x1: T else if (cond2) x2: U else x3: V
或列表的类型
List(x1: T, x2: U, x3: V)
在 repl 中查看推断的 f 类型
def f[T] = List(null: List[T], null: Set[T], null: Iterator[T])
看看我的意思。
关于scala - 为什么 productIterator 返回类型 Iterator[Any]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8610130/