在 REPL 中:
import collection.mutable.{ HashSet, SynchronizedSet }
var myPool = new HashSet[String] with SynchronizedSet[String]
myPool += "oh"
myPool += "yes"
myPool = myPool.tail
我得到:
error: type mismatch;
found : scala.collection.mutable.HashSet[String]
required: scala.collection.mutable.HashSet[String] with scala.collection.mutable.SynchronizedSet[String]
myPool = myPool.tail
^
我做错了什么?
最佳答案
正如消息所说,myPool.tail
的类型为 HashSet[String]
,并且您的变量 MyPool
声明为 HashSet [字符串] 与 SynchronizedSet[字符串]
您只需要声明您想要的类型,避免过于精确的推断类型。
var myPool : HashSet[String] = new HashSet[String] with SynchronizedSet[String]
请注意,在可变集上,tail
是一项开销很大的操作,它会返回一个新的 Set。那可能不是你想要的。 (此外,规范没有说明将删除哪个元素)
关于scala - Scala 中的 SynchronizedSet 和 set 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8475368/