在抽象类型的样式比赛

时间:2015-01-29 20:03:34

标签: scala reflection pattern-matching

trait Aggregate {
    type Command
}

class AggregateHandler(a: Aggregate) {
   def receiveCommand: Receive = {
     case a.Command => ???
   }
}

如何在a.Command上进行模式匹配?我正进入(状态; abstract type pattern AggregateHandler.this.a.Command is unchecked since it is eliminated by erasureThe outer reference in this type test cannot be checked at run time.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

例如,此Aggregate#A有一个outer字段,指向Aggregate的封闭实例。

trait Aggregate {
  //type A
  class A
}

class AggregateHandler(a: Aggregate) {
  def f: PartialFunction[Any, Unit] = {
    case _: a.A => ()
  }
}

object Test extends App {
  class X extends Aggregate {
    //type A = Int
    val x: A = new A
  }
  val x = new X
  val h = new AggregateHandler(x)
  h.f(x.x)
}