用隐式证据约束一个类

时间:2013-04-07 22:34:13

标签: scala path-dependent-type

说我有这个:

  trait Animal {
    type Species
  }

我可以很容易地编写一个只需要同一物种的两只动物的功能

def breed(a: Animal, b: Animal)(implicit evidence: a.Species =:= b.Species) = ???

但我想创建一个具有相同约束类的类:

class Bed(a: Animal, b: Animal)(implicit evidence: a.Species =:= b.Species)

但它不会编译。我尝试了一些尝试使用具有稳定标识符和约束的特征的组合,但不管怎样,但无论我做什么 - 我似乎总是遇到问题

  trait Bed {
    type T
    def a: Animal { type Species = T }
    def b: Animal { type Species = T }
  }

  object Bed {

    def apply(a1: Animal, b1: Animal)(implicit ev: a1.Species =:= b1.Species) = new Bed {
      type T = b1.Species
      def a = a1  // this line won't compile,  as the compiler can't see the two species are equal ?
      def b = b1 
    }

  }

感谢。

1 个答案:

答案 0 :(得分:4)

您可以通过Bed.apply上的类型参数表示约束,而不是通过类型等式约束

object Bed {
  def apply[T1](
    a1: Animal { type Species = T1 },
    b1: Animal { type Species = T1 }) = new Bed {
    type T = T1
    def a = a1
    def b = b1
  }
}

借助类型别名

可以做到这一点
type AnimalAux[S] = Animal { type Species = S }

object Bed {
  def apply[T1](a1: AnimalAux[T1], b1: AnimalAux[T1]) =
    new Bed {
      type T = T1
      def a = a1
      def b = b1
    }
}

示例REPL会话,

scala> trait Dog
defined trait Dog

scala> val tigger = new Animal { type Species = Dog }
tigger: Animal{type Species = Dog} = $anon$1@64bd8f9c

scala> val zebedee = new Animal { type Species = Dog }
zebedee: Animal{type Species = Dog} = $anon$1@61f2bf35

scala> Bed(tigger, zebedee)
res0: Bed{type T = Dog} = Bed$$anon$1@2b0ce330

scala> val b = Bed(tigger, zebedee)
b: Bed{type T = Dog} = Bed$$anon$1@681c81de