以下编译,但由于定义 recursize 性质,访问会fs
生成 a 。StackOverflowError
lazy val fs:List[Product] = List(2,3,4).zip(fs.tail)
如果我们想更具体地了解类型,我们可以执行以下操作:
lazy val fs:List[(Int, (Int, Product))] = List(2,3,4).zip(fs.tail)
类型不是Nothing
。由于以下内容无法编译:
scala> lazy val fs:Nothing = List(2,3,4).zip(fs.tail)
<console>:8: error: value tail is not a member of Nothing
lazy val fs:Nothing = List(2,3,4).zip(fs.tail)
如果我们将 fs 定义为 等,也会发生类似的类型错误List[Nothing]
。List[(Int, Nothing)]
所以很明显,表达式的类型是 a List
of Product
。现在如果我们Stream
改用,我们可以做一些不会导致运行时错误的东西:
scala> lazy val fs:Stream[Any] = 0 #:: 1 #:: fs.zip(fs.tail).map(p => p:Any)
fs: Stream[Any] = <lazy>
scala> fs take 5 foreach println
0
1
(0,1)
(1,(0,1))
((0,1),(1,(0,1)))