Skip to content

Instantly share code, notes, and snippets.

@huynhjl
Created October 18, 2010 06:32
Show Gist options
  • Save huynhjl/631821 to your computer and use it in GitHub Desktop.
Save huynhjl/631821 to your computer and use it in GitHub Desktop.
Closure capturing temporary objects?
// http://stackoverflow.com/questions/3956607/how-should-i-avoid-unintentionally-capturing-the-local-scope-in-function-literals
object Clos {
def method: Iterator[Int] = {
val huge = (1 to 2000000).toList
val n = huge.last
(1 to n).iterator map (_ + 1)
}
def gc() { println("GC!!"); Runtime.getRuntime.gc }
def main(args:Array[String]) {
val list = collection.mutable.ListBuffer[Iterator[Int]]()
(1 to args(0).toInt) foreach { i => list += method }
println(list.map(m => m.next).mkString(","))
println(list.map(m => m.next).mkString(","))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment