SIP-15 意味着可以使用值类来定义例如新的数字类,例如正数。是否可以编写这样一个约束,即在没有构造函数的情况下底层> 0,而不必调用单独的方法来验证约束(即;创建此类的有效实例很简洁)?
如果值类具有构造函数的概念,那么可以有如下验证的地方,但不支持(即,下面的代码将无法编译)
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