Is there a way in Scala to modify a parameter passed to a single-argument case class constructor / apply()
method before it becomes a val
? E.g.
case class AbsVal private(aVal: Double)
object AbsVal {
def apply(aVal: Double): AbsVal = AbsVal(Math.abs(aVal)) // doesn't compile
}
This fails of course with ambiguous reference to overloaded definition. I thought maybe I could trick it with named parameters (and different parameter names for the constructor vs apply()
), but that doesn't work either.
Of course instead of apply()
I could just have the private constructor and a factory method, but it's annoying to have to litter the code with AbsVal.make(x)
instead of just AbsVal(x)
.