gurjeet
gurjeet

Reputation: 129

In Scala, how does one write a class with a constructor, not all of whose arguments are class members?

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 ?

Upvotes: 4

Views: 384

Answers (1)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297155

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.

Upvotes: 4

Related Questions