title: Using sealed trait as a key for a map
body: <p>I am trying to define a map from instances of a sealed trait. In the following code, Scala seems to infer the key type as <code>Product with Serializable with Day</code>: </p>
  
  <pre><code>object Test extends App {
    sealed trait Day
    case object Sunday extends Day
    case object Monday extends Day
    case object Tuesday extends Day
  
    val m: Map[Day, Int] = Map(Sunday -&gt; 17, Monday -&gt; 4).withDefaultValue(0)
  }
  </code></pre>
  
  <p>This does not compile:</p>
  
  <pre><code>Test.scala:7: error: type mismatch;
   found   : scala.collection.immutable.Map[Product with Serializable with Test.Day,Int]
   required: Map[Test.Day,Int]
  Note: Product with Serializable with Test.Day &lt;: Test.Day, but trait Map is invariant in type A.
  You may wish to investigate a wildcard type such as `_ &lt;: Test.Day`. (SLS 3.2.10)
    val m: Map[Day, Int] = Map(Sunday -&gt; 17, Monday -&gt; 4).withDefaultValue(0)
  </code></pre>
  
  <p>I can change the key type in the definition of <code>m</code>, but that would mean repeating <code>Product with Serializable with Day</code> in many places. Another option I found was to change the definition of the trait to:</p>
  
  <pre><code>sealed trait Day extends Product with Serializable
  </code></pre>
  
  <p>As there are many advantages for using sealed traits and case objects instead of enums, I am wondering what would be a good approach to put them as keys in a map.</p>