title: Why don't generic types work with inheritance in Scala?
body: <p>So here's the code:</p>
  
  <pre><code>package week4
  object expr {
    abstract class Expr[T] {
      def eval:T = this match {
        case Number(x)   =&gt; x
        case Sum(e1, e2) =&gt; e1.eval + e2.eval
      }
      def show: String = this match {
        case Number(x)   =&gt; "" + x
        case Sum(e1, e2) =&gt; "(" + e1.show + "+" + e2.show + ")"
      }
    }
    case class Number[T](val value: T) extends Expr {
    }
    case class Sum[T](val e1: Expr[T], val e2: Expr[T]) extends Expr {
    }
  }
  </code></pre>
  
  <p>except I get the error on all for case comparisons that:</p>
  
  <blockquote>
    <p>constructor cannot be instantiated to expected type; found : week4.expr.Number[T(in class Number)] required: week4.expr.Expr[T(in
    class Expr)] Note: Nothing &lt;: T (and week4.expr.Number[T] &lt;:
    week4.expr.Expr[Nothing]), but class Expr is invariant in type T. You
    may wish to define T as +T instead.</p>
  </blockquote>
  
  <p>What am i doing wrong?</p>