【发布时间】:2012-11-25 16:34:15
【问题描述】:
假设我想要一个Stream 的正方形。一种简单的声明方法是:
scala> def squares(n: Int): Stream[Int] = n * n #:: squares(n + 1)
但是这样做会产生错误:
<console>:8: error: overloaded method value * with alternatives:
(x: Double)Double <and>
(x: Float)Float <and>
(x: Long)Long <and>
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to (scala.collection.immutable.Stream[Int])
def squares(n: Int): Stream[Int] = n * n #:: squares(n + 1)
^
那么,为什么 Scala 不能推断出 n 的类型,这显然是 Int?有人可以解释一下发生了什么吗?
【问题讨论】: