This page is no longer maintained — Please continue to the home page at

how can one avoid boxing when using function

No replies
Jesse Eichar 2
Joined: 2011-04-06,
User offline. Last seen 42 years 45 weeks ago.
Hi,  I am trying to figure out how to avoid boxing because the performance hit is killing me.  As an experiment I wrote the following code:
object X {  def main(args:Array[String]) { val b = new B[Int](3)     while(true) {      b.foreach(_ + 1)     }  }}
class B[@specialized(Int)A](val a:A) { def foreach(f: A => Unit) = f(a) }
Sadly, when I look at the byte code the foreach method is still boxing.  In my actual application I am writing a custom collection that always contains ints and I need to get around the boxing.  Any suggestions?

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland