开发者

scala pattern matching on scala.collection.script.Message

开发者 https://www.devze.com 2023-03-23 18:31 出处:网络
I am trying to cre开发者_高级运维ate a subscriber on a map. here is the code: type Msg = Message[(SomeObject)] with undoable

I am trying to cre开发者_高级运维ate a subscriber on a map.

here is the code:

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)
       }   
  }

}

in the notify above if i just print evt I get output:- Include(NoLo, someobject)..but if I try case Include the code wont compile saying found: Include required: Message

Is Include not a subclass of Message? How do you test for different messages like include, remove etc..


I can get this to compile:

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)
    }
  }
}

Funny enough, the pattern matching won't compile when Undoable is mixed in…


A little more verbose but here is what I came up with:

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)
 }
}

As you note you have to reference the elem val on subclasses of Message to get the key or the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消