我想要一个试图覆盖现有密钥值的Map。我试过了:
trait Unoverwriteable[A, B] extends scala.collection.Map[A, B] {
case class KeyAlreadyExistsException(e: String) extends Exception(e)
abstract override def + [B1 >: B] (kv: (A, B1)): Unoverwriteable[A, B1] = {
if (this contains(kv _1)) throw new KeyAlreadyExistsException(
"key already exists in WritableOnce map: %s".format((kv _1) toString)
)
super.+(kv)
}
abstract override def get(key: A): Option[B] = super.get(key)
abstract override def iterator: Iterator[(A, B)] = super.iterator
abstract override def -(key: A): Unoverwriteable[A, B] = super.-(key)
}
得到了:
<console>:11: error: type mismatch;
found : scala.collection.Map[A,B1]
required: Unoverwirteable[A,B1]
super.+(kv)
^
<console>:16: error: type mismatch;
found : scala.collection.Map[A,B]
required: Unoverwirteable[A,B]
abstract override def -(key: A): Unoverwirteable[A, B] = super.-(key)
^
我对Scala很陌生,无法找到解决这个问题的方法。有帮助吗? :)
编辑:我正在使用Scala 2.8.0.Beta1-prerelease(这会给scala.collection带来一些变化)
答案 0 :(得分:4)
由于您要覆盖Map
中的方法,因此无法将特征定义为返回类型。
最简单的解决方案是省略类型:
abstract override def + [B1 >: B] (kv: (A, B1)) = { /* ... */ }
// ...
abstract override def -(key: A) = super.-(key)
或者你可以明确地添加超类型:
import scala.collection.Map
abstract override def +[B1 >: B] (kv: (A, B1)): Map[A, B1] = { /* ... */ }
// ...
abstract override def -(key: A) = super.-(key): Map[A, B]
我认为您只需要覆盖+
,因为您的其他方法只会委托给Map
。
答案 1 :(得分:4)
这解决了编译错误:
trait Unoverwriteable[A, B] extends scala.collection.Map[A, B] {
case class KeyAlreadyExistsException(e: String) extends Exception(e)
abstract override def + [B1 >: B] (kv: (A, B1)): scala.collection.Map[A, B1] = {
if (this contains(kv _1)) throw new KeyAlreadyExistsException(
"key already exists in WritableOnce map: %s".format((kv _1) toString)
)
super.+[B1](kv)
}
abstract override def get(key: A): Option[B] = super.get(key)
abstract override def iterator: Iterator[(A, B)] = super.iterator
abstract override def -(key: A): scala.collection.Map[A, B] = super.-(key)
}
但是,我认为你真的想要装饰collection.mutable.Map#+=
,如下所示:
trait Unoverwriteable[A, B] extends collection.mutable.Map[A, B] {
case class KeyAlreadyExistsException(e: String) extends Exception(e)
abstract override def +=(kv: (A, B)): this.type = {
if (this contains (kv _1))
throw new KeyAlreadyExistsException("key already exists in WritableOnce map: %s".format((kv _1) toString))
super.+=(kv)
}
}
答案 2 :(得分:3)
您可以使用带有一点隐式魔法的scala.collection.immutable.Map来完成此操作。也就是说,您在接口中定义了一个附加方法和一个隐式转换。这是我在2.7中的表现,我确定在2.8中有不同的方法可以覆盖,但你应该得到一般的想法。
trait Unoverwriteable[A, B] extends scala.collection.immutable.Map[A, B] {
import Unoverwriteable.unoverwriteableMap
case class KeyAlreadyExistsException(e: String) extends Exception(e)
def underlying: scala.collection.immutable.Map[A, B]
def update [B1 >: B] (key: A, value: B1): Unoverwriteable[A, B1] = {
if (this contains(key)) throw new KeyAlreadyExistsException(
"key already exists in WritableOnce map: %s".format(key.toString)
)
underlying update (key, value)
}
def get(key: A): Option[B] = underlying get key
def elements: Iterator[(A, B)] = underlying.elements
def -(key: A): Unoverwriteable[A,B] = underlying - key
def empty[C]: Unoverwriteable[A,C] = underlying.empty[C]
def size: Int = underlying.size
}
然后在伴侣对象中定义隐含:
object Unoverwriteable {
implicit def unoverwriteableMap[A, B](map0: scala.collection.immutable.Map[A, B]): Unoverwriteable[A, B] =
new Unoverwriteable[A, B] { def underlying = map0 }
}
要使用它,请向地图添加Unwriteable类型注释。如果取消注释main方法中的最后两行,则会根据需要获得KeyAlreadyExistsException。
object UOMain {
def main(args: Array[String]): Unit = {
val map0 = Map((1 -> 1), (2 -> 2)): Unoverwriteable[Int, Int]
println("map0="+ map0)
val map1 = map0 - 2
println("map1="+ map1)
//val map2 = map1 + (1 -> 1000)
//println("map2" + map2)
}
}