title: Scala: Extending Map and defining +
body: <p>I'm trying to write a small wrapper class to make the <a href="http://code.google.com/p/google-gson/" rel="nofollow">Gson library</a> a bit more Scala friendly. Unfortunately, I'm running into a compile error when I try to get this going the way I would like.</p>
  
  <p>This is the code I've got so far:</p>
  
  <pre class="lang-scala prettyprint-override"><code>package com.test
  
  import com.google.gson.{JsonObject, JsonElement}
  import scala.collection.Iterator
  import scala.collection.immutable.Map
  
  case class GsonMap ( private val inner: JsonObject = new JsonObject )
      extends Map[String, JsonElement] {
  
      /** {@inheritDoc} */
      override def iterator: Iterator[(String, JsonElement)]
          = new Iterator[(String, JsonElement)] {
              private val entries = inner.entrySet.iterator
              override def hasNext: Boolean = entries.hasNext
              override def next: (String, JsonElement) = {
                  val elem = entries.next
                  ( elem.getKey, elem.getValue )
              }
          }
  
      /**
       * Returns a clone of the inner JsonObject
       */
      private def cloneInner: JsonObject = {
          val result = new JsonObject()
          iterator.foreach { (item) =&gt; result.add( item._1, item._2 ) }
          result
      }
  
      /** {@inheritDoc} */
      override def + ( kv: (String, JsonElement) ): GsonMap = {
          val cloned = cloneInner
          cloned.add( kv._1, kv._2 )
          GsonMap( cloned )
      }
  
      /** {@inheritDoc} */
      override def get( key: String ): Option[JsonElement]
          = Option( inner.get(key) )
  
      /** {@inheritDoc} */
      override def - ( key: String ): GsonMap = {
          val cloned = cloneInner
          cloned.remove( key )
          GsonMap( cloned )
      }
  
  }
  </code></pre>
  
  <p>Now, I know that the <code>+</code> method doesn't match what is defined in the Map class. That's the problem, really. I want the <code>+</code> method to accept <code>JsonElement</code>s and return a <code>GsonMap</code>, but I'm not really sure how to make that work. I've tried a few variations at this point, but with no luck</p>
  
  <p>for reference, this is the compile error I'm receiving:</p>
  
  <pre><code>[info] Compiling 1 Scala source to target/scala-2.9.2/classes...
  [error] src/main/scala/GsonMap.scala:7: class GsonMap needs to be abstract, since method + in trait Map of type [B1 &gt;: com.google.gson.JsonElement](kv: (String, B1))scala.collection.immutable.Map[String,B1] is not defined
  [error] case class GsonMap ( val inner: JsonObject = new JsonObject )
  [error]            ^
  [error] src/main/scala/GsonMap.scala:31: method + overrides nothing
  [error]     override def + ( kv: (String, JsonElement) ): GsonMap = {
  [error]                  ^
  [error] two errors found
  </code></pre>
  
  <p>Any advice out there about this?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>As was also suggested below, this is one of the variations I tried:</p>
  
  <pre class="lang-scala prettyprint-override"><code>override def +[T &gt;: JsonElement] ( kv: (String, T) ): GsonMap = {
      val cloned = cloneInner
      cloned.add( kv._1, kv._2 )
      GsonMap( cloned )
  }
  </code></pre>
  
  <p>However, it fails too:</p>
  
  <pre><code>[info] Compiling 1 Scala source to target/scala-2.9.2/classes...
  [error] /src/main/scala/GSON.scala:33: type mismatch;
  [error]  found   : T
  [error]  required: com.google.gson.JsonElement
  [error]         cloned.add( kv._1, kv._2 )
  [error]                               ^
  [error] one error found
  </code></pre>
  
  <p>My understanding of the <code>&gt;:</code> operator is that T must be a parent of JsonElement, which I don't think is what I'm looking for. In this case, this map can only ever contain instances of JsonElements, so it wouldn't be appropriate to put in parents of JsonElements.</p>