Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the iterator evaluated when creating a new Iterable from it?

> scala> val myI = new Iterable[Int]{def iterator = Iterator.continually(1)} 
> java.lang.OutOfMemoryError: Java heap space
>   at java.util.Arrays.copyOf(Arrays.java:2882)    at
> <snip>

Now, is this expected behavior? I find it somewhat strange and it gets in my way.

like image 975
ziggystar Avatar asked Jun 22 '13 12:06

ziggystar


1 Answers

This is just the REPL trying too hard to be helpful--it's trying to print out your new Iterable as part of what it does when you return a value. You can either stick it in some container that doesn't print its contents or override toString.

scala> val myI = new Iterable[Int] { def iterator = Iterator.continually(1); 
     |   override def toString = "myI" }
myI: Iterable[Int] = myI
like image 87
Rex Kerr Avatar answered Nov 24 '22 19:11

Rex Kerr