SIP-15 implies one can use value classes to define for example new numeric classes, such as positive numbers. Is it possible to code such a constraint that the underlying > 0 in absence of constructor without having to call a separate method for validating the constraint (ie; creating a valid instance of such class is succint)?
If value classes had the notion of constructor, then that could a place to have such validations such as below, but that is not supported (ie; code below will not compile)
implicit class Volatility(val underlying: Double) extends AnyVal {
require(!underlying.isNaN && !underlying.isInfinite && underlying > 0, "volatility must be a positive finite number")
override def toString = s"Volatility($underlying)"
}
Volatility(-1.0) //should ideally fail
An implicit conversion to a type marked as having passed your runtime requirement.
You can also have private methods that enforce invariants.
You could use refined to lift the validation step to compile time by refining your
Double
with refined'sPositive
predicate:Note that the last error is a compile error and not a runtime error.