1

从一些简单的代码开始:

trait Moveable[A] {
  def move(a: A): A
}
trait Animal {
  def kick[A <: Animal: Moveable](a: A): A = implicitly[Moveable[A]] move a
}
object Cat {
  implicit object CatMoveable extends Moveable[Cat] {
    def move(cat: Cat): Cat = cat copy (pos = cat.pos + 4)
  }
}
case class Cat(pos: Int) extends Animal
case class Dog(pos: Int) extends Animal
val someAnimal: Animal = Dog(0)
val kickedCat: Cat = someAnimal kick Cat(0)
println(kickedCat) // Cat(4)

我决定区分动物QuadrupedBiped动物:

trait FourFeetMoveable[A] {
  def moveWithFourFeets(a: A): A
}
trait TwoFeetMoveable[A] {
  def moveWithTwoFeets(a: A): A
}
trait Animal {
  def kick[A <: Animal /*: ??? */](a: A): A
}
trait Quadruped extends Animal {
  def kick[A <: Animal: FourFeetMoveable](a: A): A = implicitly[FourFeetMoveable[A]] moveWithFourFeets a
}
trait Biped extends Animal {
  def kick[A <: Animal: TwoFeetMoveable](a: A): A = implicitly[TwoFeetMoveable[A]] moveWithTwoFeets a
}
object Chicken {
  implicit object ChickenTwoFeetMoveable extends TwoFeetMoveable[Chicken] {
    def moveWithTwoFeets(chicken: Chicken): Chicken = chicken copy (pos = chicken.pos + 2)
  }
}
case class Dog(pos: Int) extends Quadruped
case class Chicken(pos: Int) extends Biped
val someAnimal: Animal = Dog(0)
val kickedChicken: Chicken = someAnimal kick Chicken(0)
println(kickedChicken) // Chicken(2)

必须有两个完全不同的类型类FourFeetMoveableTwoFeetMoveable,所以我不能用这样的东西抽象它们:

trait Moveable[A] {
  def move(a: A): A
}

那么如何抽象出用作上下文绑定kick在 trait中的方法的类型类Animal(参见参考资料???)?

编辑

对不起,我应该让我的例子更清楚。可以说被踢的效果可以是某种运动或某种其他动作。我想用类型类抽象出这种效果。在下面的代码中,我展示了我的意思,并且还使用了抽象类型成员KickingEffect来抽象所需的类型类,如0__建议的那样:

trait StumbleEffect[A <: Animal] {
  def stumble(a: A): A
}
trait GlideEffect[A <: Animal] {
  def glide(a: A): A
}
trait Animal {
  type KickingEffect[A <: Animal]
  def kick[A <: Animal: KickingEffect](a: A): A
}
trait Biped extends Animal {
  type KickingEffect[A <: Animal] = StumbleEffect[A]
  override def kick[A <: Animal: StumbleEffect](a: A): A = implicitly[StumbleEffect[A]] stumble a
}
trait Quadruped extends Animal {
  type KickingEffect[A <: Animal] = GlideEffect[A]
  override def kick[A <: Animal: GlideEffect](a: A): A = implicitly[GlideEffect[A]] glide a
}
object Dog {
  implicit object DogGlideEffect extends GlideEffect[Dog] {
    def glide(dog: Dog): Dog = dog copy (pos = dog.pos + 4)
  }
}
case class Dog(pos: Int) extends Quadruped
case class Cat(pos: Int) extends Quadruped
case class Chicken(pos: Int) extends Biped

但是当涉及到动物序列时,我遇到了另一个问题:

type Beast[A <: Animal, KE[_ <: Animal]] = A { type KickingEffect[X <: Animal] = KE[X] }
val dogBeast: Beast[Dog, GlideEffect] = Dog(0) // fine

type GlideBeasts[A <: Quadruped] = Beast[A, GlideEffect]
val glideBeasts: Seq[GlideBeasts[Quadruped]] = Seq(Dog(0), Cat(0)) // fine

def kickAll[A <: Animal, KE[_ <: Animal], KA <: Animal](kicker: Beast[A, KE])(animals: Seq[KA])(implicit ev: kicker.KickingEffect[KA]): Seq[KA] = {
  for (a <- animals) yield kicker kick a
}
val cat = Cat(0)
val dog = Dog(0)
kickAll(cat)(Seq(dog)) // wrong inferred kinds of type arguments
kickAll[Cat, GlideEffect, Dog](cat)(Seq(dog)) // missing implicit evidence
4

2 回答 2

2

像这样?

trait Moveable[A] {
  def move(a: A): A
}
trait FourFeetMoveable[A] extends Moveable[A]
trait TwoFeetMoveable[A] extends Moveable[A]

trait Animal {
  type CanKick[A] <: Moveable[A]
  def kick[A <: Animal : CanKick](a: A): A = implicitly[CanKick[A]] move a
}
trait Quadruped extends Animal {
  type CanKick[A] = FourFeetMoveable[A]
}
trait Biped extends Animal {
  type CanKick[A] = TwoFeetMoveable[A]
}

关于您的编辑:我建议此时不要进一步尝试使用类型对其进行建模,除非它确实是您的应用程序中非常关键的点或纯粹的思想实验。使用类型安全的设计很容易变得过于雄心勃勃,那么设计工作量与应用价值之间的比例就会越来越大;我会放弃一些编译时安全性并去寻找模式匹配和运行时错误。

如果您确实想遵循类型路线,一旦您拥有集合,您将需要HLists之类的东西来保留集合成员的各个类型。


无论如何,您可以使您的示例工作(使用显式类型参数):

def kickAll[A <: Animal, KE[_ <: Animal], KA <: Animal](
   kicker: Beast[A, KE])(animals: Seq[KA])(implicit effect: KE[KA]): Seq[KA] = {
      for (a <- animals) yield kicker kick a
}

val cat = Cat(0)
val dog = Dog(0)
kickAll(cat)(Seq(dog)) // still doesn't figure out the types
kickAll[Cat, GlideEffect, Dog](cat)(Seq(dog)) // ok!

如前所述,当您尝试使用异构列表(例如,需要不同的效果)时,会出现棘手或不可能的部分。相反,您可能会为序列的元素使用辅助类型类,以便之前可以按项目解析隐式。

于 2012-09-24T18:40:12.890 回答
1

作为旁注,我发现在真正需要它们之前不引入类型边界总是有用的。您不仅可以确保大量打字(没有双关语),而且可以保持选项打开(例如,用于以后的差异注释)。以下是完全足够的:

trait StumbleEffect[A] {
  def stumble(a: A): A
}
trait GlideEffect[A] {
  def glide(a: A): A
}
trait Animal {
  type KickingEffect[A]
  def kick[A : KickingEffect](a: A): A
}
trait Biped extends Animal {
  type KickingEffect[A] = StumbleEffect[A]
  override def kick[A : StumbleEffect](a: A): A = 
    implicitly[StumbleEffect[A]] stumble a
}
trait Quadruped extends Animal {
  type KickingEffect[A] = GlideEffect[A]
  override def kick[A : GlideEffect](a: A): A = implicitly[GlideEffect[A]] glide a
}

等等

于 2012-09-25T09:49:16.330 回答