简体   繁体   English

Scala中的SynchronizedSet和set操作

[英]SynchronizedSet and set operations in Scala

In REPL: 在REPL中:

import collection.mutable.{ HashSet, SynchronizedSet }

var myPool = new HashSet[String] with SynchronizedSet[String]
myPool += "oh"
myPool += "yes"
myPool = myPool.tail

and I get: 我得到:

error: type mismatch;
 found   : scala.collection.mutable.HashSet[String]
 required: scala.collection.mutable.HashSet[String] with scala.collection.mutable.SynchronizedSet[String]
   myPool = myPool.tail
                   ^

What am I doing wrong? 我究竟做错了什么?

Just what the message says, myPool.tail has type HashSet[String] , and your variable MyPool is declared HashSet[String] with SynchronizedSet[String] 正如消息所说的那样, myPool.tail类型为HashSet[String] ,并且您的变量MyPool HashSet[String] with SynchronizedSet[String]声明为HashSet[String] with SynchronizedSet[String]

You just need to declare the type you want to avoid the too precise inferred one. 您只需要声明要避免过于精确推断的类型。

var myPool : HashSet[String] = new HashSet[String] with SynchronizedSet[String]

Note that on a mutable set, tail is a costly operation and returns you a new Set. 请注意,在可变集上, tail是一个代价高昂的操作,并返回一个新的Set。 That might not be what you want. 那可能不是你想要的。 (Moreover, the spec is mute as to which element will be removed) (此外,规范是关于哪个元素将被删除的静音)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM