title: Scala: implementing Map with concrete types
body: <p>I'm running into some kind of quirk in the Scala type system that has me a bit stumped.  I am trying to make a class that extends Map[String,String] and I can't quite figure out how to implement the + method in such a way that the compiler accepts it.</p>
  
  <p>Here's the code I have now:</p>
  
  <pre><code>class ParamMap(val pairs:List[(String,String)] = Nil) extends Map[String,String] {
  
    lazy val keyLookup = Map() ++ pairs
  
    override def get(key: String): Option[String] = keyLookup.get(key)
    override def iterator: Iterator[(String, String)] = pairs.reverseIterator
  
    /**
     * Add a key/value pair to the map
     */
    override def + [B1 &gt;: String](kv: (String, B1)) = new ParamMap(kv :: pairs)
  
    /**
     * Remove all values for the given key from the map
     */
    override def -(key: String): ParamMap  = new ParamMap(pairs.filterNot(_._1 == key))
  
    /**
     * Remove a specific pair from the map
     */
    def -(kv: (String, String)) : ParamMap = new ParamMap(pairs - kv)
  }
  </code></pre>
  
  <p>Scala tells me this:</p>
  
  <pre><code>type mismatch;  found: (String, B1)  required: (String, String)
  </code></pre>
  
  <p>I believe this is because B1 is allowed to be a subtype of String but my constructor expects just a String (?).  My original attempt was:</p>
  
  <pre><code>override def +(kv: (String, String)) = new ParamMap(kv :: pairs)
  </code></pre>
  
  <p>But this complained because the type signature didn't match the trait:</p>
  
  <pre><code>class ParamMap needs to be abstract, since method + in trait Map of type [B1 &gt;: String](kv: (String, B1))scala.collection.immutable.Map[String,B1] is not defined
  method + overrides nothing
  </code></pre>
  
  <p>I'm new to Scala and I think I'm getting over my head here in terms of how the type system works.  Perhaps I'll try messing with casting but I have a feeling there might be a "better way" that, if I know it, will save me a lot of trouble in the future.</p>
  
  <p>Any ideas?</p>