I’m trying to understand why the following code using Scala Streams doesn’t work:
def main(args: Array[String]): Unit = {
lazy val y : SimNumericStream = y.shift
y.scalstream.take(10).print
}
class SimNumericStream( ss : Stream[Double] ) {
lazy val scalstream = ss
lazy val shift = new SimNumericStream( 0 #:: scalstream )
}
and yet replacing
lazy val y : SimNumericStream = y.shift
by
lazy val y : SimNumericStream = new SimNumericStream( 0 #:: y.scalstream )
works just fine.
I’m looking for a solution that allows to me wrap up operations on Streams inside functions without breaking the lazy evaluation when the streams are self-referential.
I have achieved the effect I would like through the following: