2

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
4

4 回答 4

3

隐式转换为标记为已通过运行时要求的类型。

scala> trait Pos
defined trait Pos

scala> implicit class P(val i: Int with Pos) extends AnyVal { def f = i }
defined class P

scala> implicit def cv(i: Int): Int with Pos = { require(i>0); i.asInstanceOf[Int with Pos] }
warning: there was one feature warning; re-run with -feature for details
cv: (i: Int)Int with Pos

scala> new P(42).f
res0: Int with Pos = 42

scala> :javap -prv -
        17: invokevirtual #35                 // Method $line5/$read$$iw$$iw$.cv:(I)I
        20: invokevirtual #38                 // Method $line4/$read$$iw$$iw$P$.f$extension:(I)I

scala> new P(-42).f
java.lang.IllegalArgumentException: requirement failed
  at scala.Predef$.require(Predef.scala:207)
  at .cv(<console>:13)
  ... 33 elided

您还可以拥有强制执行不变量的私有方法。

scala> implicit class P(val i: Int with Pos) extends AnyVal { private def g = require(i>0) ; def f = { g; i } }
defined class P

scala> new P(-42.asInstanceOf[Int with Pos]).f
java.lang.IllegalArgumentException: requirement failed
  at scala.Predef$.require(Predef.scala:207)
  at P$.$line10$$read$P$$g$extension(<console>:14)
  at P$.f$extension(<console>)
  ... 33 elided
于 2015-10-15T00:08:51.557 回答
3

您可以使用精炼来提升验证步骤以通过精炼您DoublePositive谓词来编译时间:

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)
                   ^

请注意,最后一个错误是编译错误,而不是运行时错误。

于 2015-10-16T22:21:34.193 回答
0

我完成此操作的方法是在调用案例类的构造函数“实例化”值之前使用伴随对象的.apply方法添加约束。requireprivate

警告:下面的代码不会在 REPL/Scala 工作表中编译。扩展 AnyVal 的案例类必须是顶级类;ie 不能嵌套在另一个类、特征或对象的范围内。REPL 和 Scala 工作表都是通过在执行之前将所有代码推入一个不可见的包含类来实现的。

object PositiveInt {
  def apply(value: Int): PositiveInt = {
    require(value >= 0, s"value [$value] must be greater than or equal to 0")
    new PositiveInt(value)
  }
}
case class PositiveInt private(value: Int) extends AnyVal

val positiveTestA = PositiveInt(0)
val positiveTestB = PositiveInt(1)
val positiveTestC = PositiveInt(-1) //throws required exception
于 2019-05-04T12:38:57.480 回答
0

这适用于您的用例吗?将构造函数设为私有并使用带有验证逻辑的伴随对象来创建新实例。

class User private (val userIdentifier:String) extends AnyVal {}

object User {
  def apply(userIdentifier: String): User = {
    if(Option(userIdentifier).exists(_.trim.isEmpty)) throw new IllegalArgumentException("User identifier cannot be empty!")
    new User(userIdentifier)
  }
}
于 2021-05-25T08:40:19.983 回答