私は、Java用のlaGuavaのForwardingMutableMap
特性を書き込もうとしています。ForwardingMap
これが私の最初の試みがどのように見えたかです:
trait ForwardingMutableMap[K, V, +Self <: mutable.MapLike[K, V, Self] with mutable.Map[K, V]]
extends mutable.Map[K, V]
with mutable.MapLike[K, V, Self] { this: Self =>
protected val delegate: mutable.Map[K, V]
def get(key: K): Option[V] = delegate.get(key)
def iterator: Iterator[(K, V)] = delegate.iterator
def -=(key: K): this.type = {
delegate -= key
this
}
def +=(kv: (K, V)): this.type = {
delegate += kv
this
}
}
これにより、エラーが発生します。
error: overriding method empty in trait MapLike of type => Self;
method empty in trait Map of type => scala.collection.mutable.Map[K,V] has incompatible type
trait ForwardingMutableMap[K, V, +Self <: mutable.MapLike[K, V, Self] with mutable.Map[K, V]]
2回目の試行:
trait ForwardingMutableMap[K, V, +Self <: mutable.MapLike[K, V, Self] with mutable.Map[K, V]]
extends mutable.Map[K, V]
with mutable.MapLike[K, V, Self] { this: Self =>
def empty: Self
protected val delegate: mutable.Map[K, V]
def get(key: K): Option[V] = delegate.get(key)
def iterator: Iterator[(K, V)] = delegate.iterator
def -=(key: K): this.type = {
delegate -= key
this
}
def +=(kv: (K, V)): this.type = {
delegate += kv
this
}
}
エラー:
error: overriding method empty in trait ForwardingMutableMap of type => ForwardingMutableMap.this.Self;
method empty in trait Map of type => scala.collection.mutable.Map[K,V] has incompatible type;
(Note that method empty in trait ForwardingMutableMap of type => ForwardingMutableMap.this.Self is abstract,
and is therefore overridden by concrete method empty in trait Map of type => scala.collection.mutable.Map[K,V])
trait ForwardingMutableMap[K, V, +Self <: mutable.MapLike[K, V, Self] with mutable.Map[K, V]]
3回目の試行:
trait ForwardingMutableMap[K, V, +Self <: mutable.MapLike[K, V, Self] with mutable.Map[K, V]]
extends mutable.Map[K, V]
with mutable.MapLike[K, V, Self] { this: Self =>
override def empty: Self = empty2
def empty2: Self
protected val delegate: mutable.Map[K, V]
def get(key: K): Option[V] = delegate.get(key)
def iterator: Iterator[(K, V)] = delegate.iterator
def -=(key: K): this.type = {
delegate -= key
this
}
def +=(kv: (K, V)): this.type = {
delegate += kv
this
}
}
タイプミキシングは、の代わりにForwardingMutableMap
実装する必要があります。empty2
empty
これは機能しますが、ハックのにおいがします。もっと上手くできますか?