When using range
in Scala 2.12 and proceeding to iterate over the elements why do ints get boxed into java.lang.Integer
when they are yielded? The below code allocates 10,000 Integers on the heap.
val boxedSeq = for (i <- 1 to 10000) yield i
println("Done")
If instead of yielding you print it, no Integers are created. Is the root cause that generic sequences cannot contain primitives 1?
for (i <- 1 to 10000) println(i)
println("Done")