views:

102

answers:

1

I want to write class whose constructor takes two parameters, but the arguments are not actually members of the class. e.g.

class P(V1:Int, V2:Int) {
   val set = Set(V1, V2)
}

Having constructed the 'set', I don't actually care about V1 and V2. Is there a way of expressing this in Scala ?

+2  A: 

Well, exactly like that. If the constructor arguments are not tagged with val or var, nor the class is a case class, then they'll be kept allocated if used inside methods (or lazy val, I suppose). If used just in the constructor, they won't be allocated with the object, not even as private fields.

Daniel
But the parameters will still be in scope within the class, which can be surprising:class Scope(i:Int) { def iCanSeeYou() = i + 3 }
Adam Rabung