title: Can I change the error message given by the compiler in scala?
body: <p>I have key and body case classes. Let's say the keys extend the <code>Key</code> trait and bodies extend the <code>Body</code> trait. For every key, there is exactly one given body type. Below, I have tried to program this in a way that the compiler can check this for me and it even seems to work!</p>
  
  <pre><code>trait Key [T &lt;: Body]
  trait Body
  
  case class Body1 () extends Body
  case class Body2 () extends Body
  
  case class Key1 () extends Key[Body1]
  case class Key2 () extends Key[Body2]
  
  object Tester {
    def func[T &lt;: Body](key: Key[T], body: T) = {
      println("Key: "+ key, "Body: "+body)
    }
  
    val k1 = Key1()
    val b1 = Body1()
    val b2 = Body2()
    func(k1, b1)  // good match
    func(k1, b2)  // wrong match
  }
  </code></pre>
  
  <p>As I want, the last line above gives a compile-time error (see below). But can I make it a bit more readable so that users of my library will understand what's happening? Especially, the last "hint" is misleading in my case.</p>
  
  <pre><code>Error:(18, 9) type mismatch;
   found   : A$A60.this.Key1
   required: A$A60.this.Key[Product with Serializable with A$A60.this.Body]
  Note: A$A60.this.Body1 &lt;: Product with Serializable with A$A60.this.Body (and A$A60.this.Key1 &lt;: A$A60.this.Key[A$A60.this.Body1]), but trait Key is invariant in type T.
  You may wish to define T as +T instead. (SLS 4.5)
    func(k1, b2)
         ^
  </code></pre>