Non-retained values in constructor functions of case class

Is there a way to have parameters to a case class constructor which do not become fields in the class which are retained for the life of the object? Some of the constructor parameters should be accessible from the live instance, and other (in my case) will just be clutter.

I think that in general you probably don’t want a case class if you have constructor parameters like that. But parameters to the second (and third and so on) parameter list of the constructor should not get the “case class” treatment and behave like normal class constructor parameters.

1 Like

If you have parameters that are only needed at construction time, and are never part of the object’s fields, then I would usually create a separate pseudo-constructor method in the companion object – that takes all of the params, and then builds the case class with its real fields. If that doesn’t work, it strongly suggests that, as @Jasper-M says, you don’t actually want a case class.

(The second-parameter-list trick probably works, but I don’t care much for it stylistically myself…)