In Scala, how do I write a class with a constructor, and not all of whose arguments are members of the class?

I want to write a class whose constructor takes two parameters, but the arguments are not really members of the class. eg.

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

Having built a β€œkit”, I really don't care about V1 and V2. Is there any way to express this in Scala?

+4
source share
1 answer

Well, exactly so. If the constructor arguments are not marked val or var , and the class is case class , then they will be saved if used inside methods (or lazy val , I suppose). If they are used only in the constructor, they will not be selected by the object, not even as private fields.

+4
source

Source: https://habr.com/ru/post/1307179/


All Articles