6

我有以下通用 Interval 类(由用户 soc 为我制定):

case class Interval[T](from: T, to: T)(implicit num: Numeric[T]) {
  import num.mkNumericOps // allows us to write from.toDouble and to.toDouble
  def mid: Double = (from.toDouble + to.toDouble) / 2.0
}

典型用例:Interval[Double] 或 Interval[Int]。为了添加二元联合交集(implicit num: Numeric[T])运算符,我在伴随对象中遵循了类似的模式:

object Interval {

  def union[T](interval1: Interval[T], interval2: Interval[T])(implicit num: Numeric[T]) = {
    import num.mkOrderingOps // allows interval1.from min
    Interval[T](interval1.from min interval2.from, interval1.to max interval2.to)
  }

  def intersect[T](interval1: Interval[T], interval2: Interval[T])(implicit num: Numeric[T]) = {
    import num.mkOrderingOps
    Interval[T](interval1.from max interval2.from, interval1.to min interval2.to)
  }

}  

在这两种方法中复制(implicit num: Numeric[T])and是丑陋的样板。import num.mkOrderingOps有没有办法在 Interval 对象本身的级别上只做一次?

4

2 回答 2

8

就在这里。

首先是导入。您可以改为在 Interval 范围内导入 Ordering.Implicits._ 。

object Interval {
   import Ordering.Implicits._

   def union[T](....)(implicit num: Numeric[T]) = {
     // do not import num.mkOrderingOps
     ...
   }
   ...
}

有了这些隐式,当它找到一个排序操作时,它会在操作发生的范围内寻找隐式排序(数字是排序)。在您的每个例程中,恰好都有一个适当的隐含范围。如果您也需要算术运算,还可以导入 Numeric.Implicits._

现在有了隐含的论点。语言中有一个快捷方式,称为上下文绑定:您可以编写def f[T: X](args)而不是def f[T](args)(implicit someName: X[T])

不同之处在于您没有上下文绑定的隐式名称(您可以使用 implictly[T] 但这几乎不会更短。幸运的是,您不再需要使用 import Ordering.Implicits._ 的名称

所以

object Interval {
   import Ordering.Implicits._
   // also import Numeric.Implicits._ if you need +,-,*,/ ...
   def union[T: Numeric] ...
   def intersection[T: Numeric] ...
}
于 2012-05-13T08:50:58.543 回答
6

Numeric对象中类型类的使用Interval有一个类型参数T,该参数必须绑定在其封闭范围内的某个位置。Interval,作为唯一的常量值,不能提供该绑定。

这个特定问题的一种解决方案是将您的unionintersect操作的定义作为普通实例方法移动到Interval 中,在这种情况下,它们将与类的其余部分共享绑定T和关联实例,Numeric

case class Interval[T : Numeric](from: T, to: T) {
  import Numeric.Implicits._
  import Ordering.Implicits._

  def mid: Double = (from.toDouble + to.toDouble) / 2.0
  def union(interval2: Interval[T]) =
    Interval(this.from min interval2.from, this.to max interval2.to)
  def intersect(interval2: Interval[T]) =
    Interval(this.from max interval2.from, this.to min interval2.to)
}

Interval但是,如果您希望将这些操作的定义与类分开,那么一种减少您需要在 API 中追踪的隐式样板数量的方法是根据 Numeric[ T]。例如,

// Type class supplying union and intersection operations for values
// of type Interval[T]
class IntervalOps[T : Numeric] {
  import Ordering.Implicits._

  def union(interval1: Interval[T], interval2: Interval[T]) =
    Interval[T](interval1.from min interval2.from, interval1.to max interval2.to)

  def intersect(interval1: Interval[T], interval2: Interval[T]) =
    Interval[T](interval1.from max interval2.from, interval1.to min interval2.to)
}

implicit def mkIntervalOps[T : Numeric] = new IntervalOps[T]

在使用中看起来像,

def use[T](i1 : Interval[T], i2 : Interval[T])(implicit ops : IntervalOps[T]) = {
  import ops._
  val i3 = union(i1, i2)
  val i4 = intersect(i1, i2)
  (i3, i4)
}

第三个选项结合了这两个,使用隐式定义来丰富原始类和附加方法,

class IntervalOps[T : Numeric](interval1 : Interval[T]) {
  import Ordering.Implicits._

  def union(interval2: Interval[T]) =
    Interval[T](interval1.from min interval2.from, interval1.to max interval2.to)

  def intersect(interval2: Interval[T]) =
    Interval[T](interval1.from max interval2.from, interval1.to min interval2.to)
}

implicit def enrichInterval[T : Numeric](interval1 : Interval[T]) =
  new IntervalOps[T](interval1)

type Ops[T] = Interval[T] => IntervalOps[T]

那么在使用中,

def use[T](i1 : Interval[T], i2 : Interval[T])(implicit ops : Ops[T]) = {
  val i3 = i1 union i2
  val i4 = i1 intersect i2
  (i3, i4)
}
于 2012-05-13T08:46:30.983 回答