使用 Scala 的 ObservableMap
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是 MyMap 没有细化 Pub 类型,因此 MyMap 需要一个 Subscriber[Message[(Int, Int)] with Undoable, ObservableMap[Int, Int]] 并且您的订阅者类型是
订阅者[消息[(Int, Int)],可撤消,MyMap]
。有两个选项 - 更改订阅者以使Pub
类型为ObservableMap[Int, Int]
:或者更改 MyMap 以覆盖 Pub 类型:
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 isSubscriber[Message[(Int, Int)] with Undoable, MyMap]
. There are two options -- either change your subscriber so that thePub
type isObservableMap[Int, Int]
:or, change MyMap to override the Pub type: