我想将对象的方法存储在可变集合中。我可以毫无问题地添加该方法,但无法删除它:
scala> import scala.collection.mutable.Set
import scala.collection.mutable.Set
scala> class c {
| def f(value: Double) {
| println(value)
| }
| }
defined class c
scala> val o = new c
o: c = c@4b08e64d
scala> val s = Set.empty[Function1[Double, Unit]]
s: scala.collection.mutable.Set[Double => Unit] = Set()
scala> s add o.f
res0: Boolean = true
scala> s.size
res1: Int = 1
scala> s remove o.f
res2: Boolean = false
scala> s.size
res3: Int = 1
如果我创建一个用于保存 c 类型对象的集合,那么一切都会按预期工作。
scala> val s2 = Set.empty[c]
s2: scala.collection.mutable.Set[c] = Set()
scala> s2 add o
res4: Boolean = true
scala> s2.size
res5: Int = 1
scala> s2 remove o
res6: Boolean = true
scala> s2.size
res7: Int = 0
我想做的事可能吗?