为什么scala Tuple productIterator返回类型Iterator [Any]?
例如,如果Tuple3或Product3是 productIterator,则定义如下
def productIterator[T1<:X,T2<:X,T3<:X,X] = Iterator(_1,_2,_3)
Run Code Online (Sandbox Code Playgroud)
以下Expression可以返回Iterator [java.lang.Number]
(BigInt(1),new java.lang.Long(2),new java.lang.Float(3)).productIterator
Run Code Online (Sandbox Code Playgroud)
但目前的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>
Run Code Online (Sandbox Code Playgroud)
更新
它是乘积元素(未擦除)类型的最小上限。例如,在 a 中,Product[T, U, V]它与表达式的类型相同
if (cond1) x1: T else if (cond2) x2: U else x3: V
Run Code Online (Sandbox Code Playgroud)
或列表的类型
List(x1: T, x2: U, x3: V)
Run Code Online (Sandbox Code Playgroud)
在 repl 中查看 f 的推断类型
def f[T] = List(null: List[T], null: Set[T], null: Iterator[T])
Run Code Online (Sandbox Code Playgroud)
看看我的意思。
| 归档时间: |
|
| 查看次数: |
1185 次 |
| 最近记录: |