Type will be erased by compiler.
Compiler warning message:
Warning:(21, 13) non-variable type argument Int in type pattern A[Int] is unchecked since it is eliminated by erasure case a: A[Int] => println("Int" + a.getClass) ^ Warning:(22, 13) non-variable type argument scala.util.Try[Int] in type pattern A[scala.util.Try[Int]] is unchecked since it is eliminated by erasure case a: A[Try[Int]] => println("TryInt" + a.getClass) ^
Output:
Intclass AInt Intclass ATryInt
Code:
import scala.util.Try trait A[T] { } class AInt extends A[Int] { } class ATryInt extends A[Try[Int]] object Test extends App { val a = new AInt() val aTryInt = new ATryInt() p(a) p(aTryInt) def p[T](a: A[T]) = a match { case a: A[Int] => println("Int" + a.getClass) case a: A[Try[Int]] => println("TryInt" + a.getClass) } }
时间: 2024-12-29 16:04:57