This code compiles with an error:
def f1[T](e: T): T = e match { case i:Int => i case b:Boolean => b } // type mismatch; // found : i.type (with
In addition to @Esardes answer, this worked by defining a type bound for T:
T
scala> def f1[T >: AnyVal](e: T):T = e match { | case i:Int => i | case b:Boolean => b | } f1: [T >: AnyVal](e: T)T scala> f1(1) res3: AnyVal = 1 scala> f1(true) res4: AnyVal = true