A variable used in its own definition?

2020-06-09 07:08发布

问题:

An infinite stream:

val ones: Stream[Int] = Stream.cons(1, ones)

How is it possible for a value to be used in its own declaration? It seems this should produce a compiler error, yet it works.

回答1:

It's not always a recursive definition. This actually works and produces 1:

val a : Int = a + 1
println(a)

variable a is created when you type val a: Int, so you can use it in the definition. Int is initialized to 0 by default. A class will be null.

As @Chris pointed out, Stream accepts => Stream[A] so a bit another rules are applied, but I wanted to explain general case. The idea is still the same, but the variable is passed by-name, so this makes the computation recursive. Given that it is passed by name, it is executed lazily. Stream computes each element one-by-one, so it calls ones each time it needs next element, resulting in the same element being produces once again. This works:

val ones: Stream[Int] = Stream.cons(1, ones)
println((ones take 10).toList) // List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

Though you can make infinite stream easier: Stream.continually(1) Update As @SethTisue pointed out in the comments Stream.continually and Stream.cons are two completely different approaches, with very different results, because cons takes A when continually takes =>A, which means that continually recomputes each time the element and stores it in the memory, when cons can avoid storing it n times unless you convert it to the other structure like List. You should use continually only if you need to generate different values. See @SethTisue comment for details and examples.

But notice that you are required to specify the type, the same as with recursive functions.

And you can make the first example recursive:

lazy val b: Int = b + 1
println(b)

This will stackoverflow.



回答2:

Look at the signature of Stream.cons.apply:

apply[A](hd: A, tl: ⇒ Stream[A]): Cons[A]

The on the second parameter indicates that it has call-by-name semantics. Therefore your expression Stream.cons(1, ones) is not strictly evaluated; the argument ones does not need to be computed prior to being passed as an argument for tl.



回答3:

The reason this does not produce a compiler error is because both Stream.cons and Cons are non-strict and lazily evaluate their second parameter.

ones can be used in it's own definition because the object cons has an apply method defined like this:

/** A stream consisting of a given first element and remaining elements
 *  @param hd   The first element of the result stream
 *  @param tl   The remaining elements of the result stream
 */
def apply[A](hd: A, tl: => Stream[A]) = new Cons(hd, tl)

And Cons is defined like this:

final class Cons[+A](hd: A, tl: => Stream[A]) extends Stream[A]

Notice that it's second parameter tl is passed by name (=> Stream[A]) rather than by value. In other words, the parameter tl is not evaluated until it is used in the function.

One advantage to using this technique is that you can compose complex expressions that may be only partially evaluated.