0
  def fix[A, B](fn : Function2[Function1[A, B], A, B]) : Function1[A, B] =
    (x : A) => fn(fix(fn), x)

  lazy val fibs1 = fix[(Int, Int), Stream[Int]](
    (fn, a) => a._1 #:: fn((a._2, a._1 + a._2))
  )

  val fibs2 = fix[(Int, Int), Stream[Int]](
    (fn, a) => a._1 #:: fn((a._2, a._1 + a._2))
  )

While learning Scala I ran into a strange error. Why is it that fibs1((1,1)) produces no error, while fibs2((1, 1)) gives a null pointer exception?

EDIT:

This code was inside a App class. It seems like vals are not initialized until instance.main(...) is evaluated. I assume the implementation for lazy vals is different.

Carl
  • 13
  • 5

1 Answers1

0

you are right. some val was not initialized, but since you make it a lazy val, it was only evaluated when needed, and at that time it was already initialized by some code you don't control

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82