There is a simple workaround. Just wrap your tail recursive stream consumer in another function that receives the stream via a by-name parameter:
import scala.annotation.tailrec
trait T {
def consume[A](as: => Stream[A]): Unit = {
@tailrec
def loop[A](as: Stream[A]): Unit = {
if (as.isEmpty) ()
else loop(as.tail)
}
loop(as)
}
}
object O extends T {
def main(args: Array[String]): Unit =
O.consume(Range(1, 1000000000).toStream)
}
The forwarder method will hold a reference to a function computing an expression the result of which is a stream:
public final class O$ implements T {
public static final MODULE$;
// This is the forwarder:
public <A> void consume(Function0<Stream<A>> as) {
T.class.consume(this, as);
}
. . .
public void main(String[] args) {
consume(new AbstractFunction0() {
public final Stream<Object> apply() {
return package..MODULE$.Range().apply(1, 1000000000).toStream();
}
});
}
}