为什么productIterator返回类型迭代器[Any]?
问题描述:
为什么scala元组productIterator返回类型Iterator [Any]?为什么productIterator返回类型迭代器[Any]?
例如,如果如下定义Tuple3或Product3 productIterator
def productIterator[T1<:X,T2<:X,T3<:X,X] = Iterator(_1,_2,_3)
以下表达式可以返回迭代[java.lang.Number中]
(BigInt(1),new java.lang.Long(2),new java.lang.Float(3)).productIterator
但是,目前的阶版本(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>
UPDATE
它是最小上限(未擦除)类型产品元件。例如,在一个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])
类型
看看我的意思。
你能解释一下吗?它是因为2.10还是因为你注释了上限? – ziggystar 2011-12-23 09:16:35
scala3 -X实验 – extempore 2011-12-23 13:52:40
哦太棒了!谢谢 – 2011-12-24 04:16:07