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

发布于 2024-11-26 13:39:45 字数 465 浏览 1 评论 0原文

我正在尝试在地图上创建订阅者。

这是代码:

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 代码将无法编译说找到: Include required: Message

Is Include not a subclass of信息?如何测试不同的消息,例如包含、删除等。

I am trying to create 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慈悲佛祖 2024-12-03 13:39:45

我可以编译这个:

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

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…

北笙凉宸 2024-12-03 13:39:45

更详细一点,但这是我想到的:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文