I studied some posts in scala infinite streams to better wrap my lead this concept. I liked the simple solution in this post , which I reproduced below:
def fib: Stream[Long] = { def tail(h: Long, n: Long): Stream[Long] = h
My initial understanding of what was happening is that we are returning a Stream [Long] object with a tail method. To test this (seemingly incorrect) hypothesis, I did the following, which will not compile:
def fib: Stream[Long] = { override def tail(h: Long, n: Long): Stream[Long] = h
So this solution seems to be based on redefinition. So now I'm wondering what exactly happens with scala constructs that have def of some type 'T', where the value of this block contains another def, which, at first glance, overrides the T method?
Thank you in advance for your enlightenment!
EDIT is the result of a survey of the solution in an excellent answer from Mateusz Dymczyk:
object Foolaround extends App { def fib: Stream[Long] = { def pail(h: Long, n: Long): Stream[Long] = h
source share