scalavalue-class

validations in value classes


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

Solution

  • You could use refined to lift the validation step to compile time by refining your Double with refined's Positive predicate:

    import eu.timepit.refined.auto._
    import eu.timepit.refined.numeric._
    import shapeless.tag.@@
    
    scala> implicit class Volatility(val underlying: Double @@ Positive) extends AnyVal
    defined class Volatility
    
    scala> Volatility(1.5)
    res1: Volatility = Volatility@3ff80000
    
    scala> Volatility(-1.5)
    <console>:52: error: Predicate failed: (-1.5 > 0).
           Volatility(-1.5)
                       ^
    

    Note that the last error is a compile error and not a runtime error.