隐式参数解析——设置优先级

发布于 2024-11-28 00:31:16 字数 1240 浏览 3 评论 0原文

我正在尝试创建一个类型类 Default ,它为给定类型提供默认值。以下是我到目前为止所得出的结论:

trait Default[A] {
  def value: A
}

object Default {
  def withValue[A](a: A) = new Default[A] {
    def value = a
  }

  def default[A : Default]: A = implicitly[Default[A]].value

  implicit val forBoolean = withValue(false)

  implicit def forNumeric[A : Numeric] = 
    withValue(implicitly[Numeric[A]].zero)

  implicit val forChar = withValue(' ')

  implicit val forString = withValue("")

  implicit def forOption[A] = withValue(None : Option[A])

  implicit def forAnyRef[A >: Null] = withValue(null : A)
}

case class Person(name: String, age: Int)

case class Point(x: Double, y: Double)

object Point {
  implicit val pointDefault = Default withValue Point(0.0, 0.0)
}

object Main {
  def main(args: Array[String]): Unit = {
    import Default.default
    println(default[Int])
    println(default[BigDecimal])
    println(default[Option[String]])
    println(default[String])
    println(default[Person])
    println(default[Point])
  }
}

上述实现的行为符合预期,除了 BigInt 和 BigDecimal 的情况(以及作为 实例的其他用户定义类型) Numeric),它给出 null 而不是零。我应该怎么做才能使 forNumeric 优先于 forAnyRef 并且获得我期望的行为?

I am trying to create a typeclass Default that supplies the default value for a given type. Here is what I have come up with so far:

trait Default[A] {
  def value: A
}

object Default {
  def withValue[A](a: A) = new Default[A] {
    def value = a
  }

  def default[A : Default]: A = implicitly[Default[A]].value

  implicit val forBoolean = withValue(false)

  implicit def forNumeric[A : Numeric] = 
    withValue(implicitly[Numeric[A]].zero)

  implicit val forChar = withValue(' ')

  implicit val forString = withValue("")

  implicit def forOption[A] = withValue(None : Option[A])

  implicit def forAnyRef[A >: Null] = withValue(null : A)
}

case class Person(name: String, age: Int)

case class Point(x: Double, y: Double)

object Point {
  implicit val pointDefault = Default withValue Point(0.0, 0.0)
}

object Main {
  def main(args: Array[String]): Unit = {
    import Default.default
    println(default[Int])
    println(default[BigDecimal])
    println(default[Option[String]])
    println(default[String])
    println(default[Person])
    println(default[Point])
  }
}

The above implementation behaves as expected, except for the cases of BigInt and BigDecimal (and other user defined types that are instances of Numeric) where it gives null instead of zero. What should I do so that forNumeric takes precedence over forAnyRef and I get the behavior I expect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经暖 2024-12-05 00:31:16

选择 forAnyRef 隐式是因为根据 Scala 参考的第 §6.26.3“重载解析”,它比 forNumeric更具体。有一种方法可以通过将其移动到 Default 扩展的特征来降低其优先级,如下所示:

trait LowerPriorityImplicits extends LowestPriorityImplicits {
  this: Default.type =>

  implicit def forAnyRef[A >: Null] = withValue(null: A)

}

object Default extends LowerPriorityImplicits {
  // as before, without forAnyRef
}

但这只是技巧的一部分,因为现在 forAnyRef>forNumeric 彼此一样具体,您将收到一个不明确的隐式错误。这是为什么?嗯,forAnyRef 获得了一个额外的特异性点,因为它对 A 有一个重要的约束:A >: Null。然后,您可以做的就是向 forNumeric 添加一个重要的约束,将其在 Default 中加倍:

implicit def forNumericVal[A <: AnyVal: Numeric] = withValue(implicitly[Numeric[A]].zero)

implicit def forNumericRef[A <: AnyRef: Numeric] = withValue(implicitly[Numeric[A]].zero)

现在,这个附加约束使 forNumericVal 和对于可用 Numeric 的类型,forNumericRefforAnyRef 更具体。

The forAnyRef implicit is chosen because it is more specific than forNumeric according to §6.26.3 “Overloading Resolution” of the Scala reference. There is a way to reduce its priority by moving it to a trait that Default extends, like this:

trait LowerPriorityImplicits extends LowestPriorityImplicits {
  this: Default.type =>

  implicit def forAnyRef[A >: Null] = withValue(null: A)

}

object Default extends LowerPriorityImplicits {
  // as before, without forAnyRef
}

But that's only part of the trick, because now both forAnyRef and forNumeric are as specific as each other, and you'll get an ambiguous-implicit error. Why is that? Well, forAnyRef gets an extra specificity point because it has a non-trivial constraint on A: A >: Null. What you can do then, to add a nontrivial constraint to forNumeric, is to double it in Default:

implicit def forNumericVal[A <: AnyVal: Numeric] = withValue(implicitly[Numeric[A]].zero)

implicit def forNumericRef[A <: AnyRef: Numeric] = withValue(implicitly[Numeric[A]].zero)

Now, this additional constraint makes forNumericVal and forNumericRef more specific that forAnyRef for types where a Numeric is available.

深居我梦 2024-12-05 00:31:16

这是解决问题的另一种方法,不需要任何重复代码:

trait Default[A] {
  def value: A
}

object Default extends LowPriorityImplicits {
  def withValue[A](a: A) = new Default[A] {
    def value = a
  }

  def default[A : Default]: A = implicitly[Default[A]].value

  implicit val forBoolean = withValue(false)

  implicit def forNumeric[A : Numeric] = 
    withValue(implicitly[Numeric[A]].zero)

  implicit val forChar = withValue(' ')

  implicit val forString = withValue("")

  implicit def forOption[A] = withValue(None : Option[A])
}

trait LowPriorityImplicits { this: Default.type =>
  implicit def forAnyRef[A](implicit ev: Null <:< A) = withValue(null : A)
}

case class Person(name: String, age: Int)

case class Point(x: Double, y: Double)

object Point {
  implicit val pointDefault = Default withValue Point(0.0, 0.0)
}

object Main {
  import Default.default

  def main(args: Array[String]): Unit = {
    println(default[Int])
    println(default[BigDecimal])
    println(default[Option[String]])
    println(default[String])
    println(default[Person])
    println(default[Point])
  }
}

Here is another way to solve the problem, doesn't require any code duplication:

trait Default[A] {
  def value: A
}

object Default extends LowPriorityImplicits {
  def withValue[A](a: A) = new Default[A] {
    def value = a
  }

  def default[A : Default]: A = implicitly[Default[A]].value

  implicit val forBoolean = withValue(false)

  implicit def forNumeric[A : Numeric] = 
    withValue(implicitly[Numeric[A]].zero)

  implicit val forChar = withValue(' ')

  implicit val forString = withValue("")

  implicit def forOption[A] = withValue(None : Option[A])
}

trait LowPriorityImplicits { this: Default.type =>
  implicit def forAnyRef[A](implicit ev: Null <:< A) = withValue(null : A)
}

case class Person(name: String, age: Int)

case class Point(x: Double, y: Double)

object Point {
  implicit val pointDefault = Default withValue Point(0.0, 0.0)
}

object Main {
  import Default.default

  def main(args: Array[String]): Unit = {
    println(default[Int])
    println(default[BigDecimal])
    println(default[Option[String]])
    println(default[String])
    println(default[Person])
    println(default[Point])
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文