scala.collection.script.Message上的scala模式匹配

scala.collection.script.Message上的scala模式匹配,scala,scala-collections,Scala,Scala Collections,我正在尝试在地图上创建订户 代码如下: 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) } } } 在上面的通知中,如

我正在尝试在地图上创建订户

代码如下:

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,我会得到输出:-Include(NoLo,someobject)…但是如果我尝试case Include,代码将不会编译为find:Include required:Message


Include不是Message的子类吗?如何测试包括、删除等不同消息。

我可以编译以下内容:

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
混合在一起时,模式匹配将无法编译…

我可以编译以下内容:

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
混合在一起时,模式匹配将无法编译…

稍微详细一点,但我想到的是:

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来获取键或值。

稍微详细一点,但下面是我想到的:

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来获取键或值。

我能够使用evt match{case x:Include[(K,V)]=>println(x.elem.\u 1)让它工作,使用evt match{case x:Include[(K,V)]=>println(x.elem.\u 1)问题是您不应该将某个内容与
案例x:x[A]
匹配,因为有关
A
参数化的信息由于擦除而丢失。在这种情况下,它是安全的,但不是在一般情况下。问题是您不应该将某个内容与
案例x:x[A]匹配
因为有关
A
参数化的信息由于擦除而丢失。在这种情况下,它是安全的,但在一般情况下不安全。