使用 Scala 的 ObservableMap

发布于 2024-11-18 23:21:28 字数 1340 浏览 4 评论 0原文

我正在尝试使用 scala.collection.mutable.ObservableMap。

我从 scala-user 中获取了下面的代码段 并复制到 REPL。

该电子邮件提到票证 2704 已被标记为已修复,但此代码段不起作用。

那么语法是否发生了变化或者 subscribe 被错误地调用了?

这是 2.9.0.final 上的

scala> import scala.collection.mutable._

import scala.collection.mutable._

scala> import scala.collection.script._
import scala.collection.script._

scala> class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int,MyMap]
<console>:13: error: wrong number of type arguments for scala.collection.mutable.ObservableMap, should be 2
       class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int,MyMap]
                                                 ^

scala> class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int]
defined class MyMap

scala> val map = new MyMap
map: MyMap = Map()

scala> class MySub extends Subscriber[Message[(Int,Int)],MyMap] {
     | def notify(pub: MyMap, evt: Message[(Int,Int)]) { println(evt) }
     | }
defined class MySub

scala> val sub = new MySub
sub: MySub = MySub@49918c8f

scala> map.subscribe(sub)
<console>:18: error: type mismatch;
 found   : MySub
 required: map.Sub
       map.subscribe(sub)

I'm trying to use scala.collection.mutable.ObservableMap.

I grabbed the snippet below from scala-user and copied it to the REPL.

The email mentions ticket 2704 which has been marked as fixed but this snippet does not work.

So has the syntax changed or is subscribe being called incorrectly?

This is on 2.9.0.final

scala> import scala.collection.mutable._

import scala.collection.mutable._

scala> import scala.collection.script._
import scala.collection.script._

scala> class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int,MyMap]
<console>:13: error: wrong number of type arguments for scala.collection.mutable.ObservableMap, should be 2
       class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int,MyMap]
                                                 ^

scala> class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int]
defined class MyMap

scala> val map = new MyMap
map: MyMap = Map()

scala> class MySub extends Subscriber[Message[(Int,Int)],MyMap] {
     | def notify(pub: MyMap, evt: Message[(Int,Int)]) { println(evt) }
     | }
defined class MySub

scala> val sub = new MySub
sub: MySub = MySub@49918c8f

scala> map.subscribe(sub)
<console>:18: error: type mismatch;
 found   : MySub
 required: map.Sub
       map.subscribe(sub)

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

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

发布评论

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

评论(1

柠檬色的秋千 2024-11-25 23:21:28

这里的问题是 MyMap 没有细化 Pub 类型,因此 MyMap 需要一个 Subscriber[Message[(Int, Int)] with Undoable, ObservableMap[Int, Int]] 并且您的订阅者类型是订阅者[消息[(Int, Int)],可撤消,MyMap]。有两个选项 - 更改订阅者以使 Pub 类型为 ObservableMap[Int, Int]

import scala.collection.mutable._
import scala.collection.script._

class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int]
val map = new MyMap

class MySub extends Subscriber[Message[(Int,Int)] with Undoable, ObservableMap[Int, Int]] {
  def notify(pub: ObservableMap[Int, Int], evt: Message[(Int,Int)] with Undoable) { println(evt) }
}
val sub = new MySub

map.subscribe(sub)

或者更改 MyMap 以覆盖 Pub 类型:

import scala.collection.mutable._
import scala.collection.script._

class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int] {
  type Pub = MyMap
}
val map = new MyMap

class MySub extends Subscriber[Message[(Int,Int)] with Undoable, MyMap] {
  def notify(pub: MyMap, evt: Message[(Int,Int)] with Undoable) { println(evt) }
}
val sub = new MySub

The problem here is that MyMap isn't refining the Pub type, so MyMap wants a Subscriber[Message[(Int, Int)] with Undoable, ObservableMap[Int, Int]] and your subscriber type is Subscriber[Message[(Int, Int)] with Undoable, MyMap]. There are two options -- either change your subscriber so that the Pub type is ObservableMap[Int, Int]:

import scala.collection.mutable._
import scala.collection.script._

class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int]
val map = new MyMap

class MySub extends Subscriber[Message[(Int,Int)] with Undoable, ObservableMap[Int, Int]] {
  def notify(pub: ObservableMap[Int, Int], evt: Message[(Int,Int)] with Undoable) { println(evt) }
}
val sub = new MySub

map.subscribe(sub)

or, change MyMap to override the Pub type:

import scala.collection.mutable._
import scala.collection.script._

class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int] {
  type Pub = MyMap
}
val map = new MyMap

class MySub extends Subscriber[Message[(Int,Int)] with Undoable, MyMap] {
  def notify(pub: MyMap, evt: Message[(Int,Int)] with Undoable) { println(evt) }
}
val sub = new MySub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文