可以说我想要一个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
显然是 a的类型Int
?有人可以解释发生了什么吗?