2

I am reading a Scala textbook regarding Streams

sealed trait Stream[+A]
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]

The textbook reads

"Due to technical limitations, these are thunks that must be explicitly forced, rather than by-name parameters. "

I suppose the textbook means that h: ()=>A, t: ()=>Stream[A] should not be replaced with h: =>A, t: =>Stream[A]. But what are the so-called "technical limitations" that prohibit this?

zell
  • 9,830
  • 10
  • 62
  • 115

1 Answers1

4

But what are the so-called "technical limitations" that prohibit this?

The technical limitations is that currently (as of Scala 2.12.x), Scala doesn't support lazy val parameters or by name parameters on case classes.

Why? Due to the fact that case classes get automatic method implementations derived for them by the compiler, such as equals and hashCode, for example, which are computed off of the case classes values. How would one compute a hashCode value, for say, an infinite stream?

A fuller answer on why case classes can't have by name parameters can be found at Scala case class prohibits call-by-name parameters?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321