我正在阅读一本有关 Streams 的 Scala 教科书
sealed trait Stream[+A]
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
课本上写着
"Due to technical limitations, these are thunks that must be explicitly forced, rather than by-name parameters. "
我认为教科书的意思是 h: ()=>A, t: ()=>Stream[A]
不应替换为 h: =>A, t: =>流[A]
。但禁止这样做的所谓“技术限制”是什么?
最佳答案
But what are the so-called "technical limitations" that prohibit this?
技术限制是,目前(从 Scala 2.12.x 开始),Scala 不支持 lazy val
参数或 case 类上的名称参数。 p>
为什么?由于案例类获得由编译器派生的自动方法实现,例如 equals
和 hashCode
,它们是根据案例类计算出来的< em>值。如何计算 hashCode 值(例如无限流)?
关于为什么案例类不能按名称参数的更完整答案可以在 Scala case class prohibits call-by-name parameters? 找到。
关于scala - 为什么在 Scala 中定义 Streams 时必须显式强制使用 thunk?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48803972/