I want a Map that throws on attempt to overwrite a value for existing key. I tried:
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)
}
and got:
<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)
^
I'm quite new to Scala and can't figure out a way to overcome this. Any help? :)
edit: I'm using Scala 2.8.0.Beta1-prerelease (which brings some changes to scala.collection)