gpt4 book ai didi

scala - Scala 中的可观察 map

转载 作者:行者123 更新时间:2023-12-05 09:25:00 25 4
gpt4 key购买 nike

我是 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()

关于scala - Scala 中的可观察 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6848537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com