我正在尝试在地图上创建订阅者。
这是代码:
type Msg = Message[(SomeObject)] with undoable
class mySub extends Subscriber[Msg,HashMap] {
def notify(pub:HashMap, evt: Msg) = {
evt match{
case Include(NoLo,x) => println(x)
}
}
}
在上面的通知中,如果我只是打印evt我得到输出: - 包括(NoLo,someobject)..但如果我尝试案例包括代码不会编译说找到:包括所需:消息
是否包含Message的子类?你如何测试不同的消息,如包括,删除等..
答案 0 :(得分:1)
我可以编译:
import collection.mutable._
import collection.script._
type K = Int
type V = Int
type Msg = Message[(K, V)] with Undoable
class mySub extends Subscriber[Msg, HashMap[K, V]] {
def notify(pub: HashMap[K, V], evt: Msg) = {
(evt: Message[(K, V)]) match {
case Include(NoLo, x) => println(x)
}
}
}
有趣的是,当Undoable
混合在......时,模式匹配将无法编译。
答案 1 :(得分:0)
稍微冗长一点,但这就是我想出的:
import scala.collection.mutable.{HashMap, Subscriber, Publisher, Undoable, ObservableMap}
import scala.collection.script.{Message, Update, Include, Reset, Remove, Script}
class MySub extends Subscriber[Message[(Int,Int)] with Undoable, ObservableMap[Int, Int]] {
def notify(pub: ObservableMap[Int, Int], evt: Message[(Int, Int)] with Undoable) = evt match {
case update: Update[(Int, Int)] => println("Update: " + update )
case include: Include[(Int, Int)] => println("Include: " + include )
case reset: Reset[(Int, Int)] => println("Reset:" + reset)
case remove: Remove[(Int, Int)] => println("Remove: " + remove)
case script: Script[(Int, Int)] => println("Sript: " + script)
}
}
如您所知,您必须在Message的子类上引用elem val来获取键或值。