开发者

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 ?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜