我是 Scala 的新手。我想在修改 map 时收到通知。我认为这可以使用可观察 map 来完成。
我正在尝试定义一个如下所示的对象
var myObj = new Map[UUID, MyType] with ObservableMap[UUID,MyType]
但它没有编译说..
error: object creation impossible, since:
method iterator in trait MapLike of type => Iterator[(java.util.UUID, MyType)] is not defined
method get in trait MapLike of type (key: java.util.UUID)Option[MyType] is not defined
method -= in trait ObservableMap of type (key: java.util.UUID)this.type is marked `abstract' and `override', but no concrete implementation could be found in a base class
method += in trait ObservableMap of type (kv: (java.util.UUID, MyType))this.type is marked `abstract' and `override', but no concrete implementation could be found in a base class
为什么会这样?如何实例化 ObservableMap?
最佳答案
您需要将 ObservableMap
与具体的 map 类型混合。
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> val map = new HashMap[Int, Int] with ObservableMap[Int, Int]
map: scala.collection.mutable.HashMap[Int,Int] with scala.collection.mutable.ObservableMap[Int,Int] = Map()
https://stackoverflow.com/questions/6848537/