【发布时间】:2020-09-24 00:49:09
【问题描述】:
我对 Scala 中的子类型感到困惑。我的主要问题是如何区分C[T1] 和C[T2]。有两种情况:
-
C[T1]"等于"C[T2]因为它们都是C的子类型。 -
C[T1]不“等于”C[T2],因为C[T1]和C[T2]最终是不同的类型。
我试过.getClass之类的方法,似乎这个策略行不通,因为我们有原始类型。
println(List[Int](1).getClass == List[Double](1.0).getClass) // True
println(List[Int](1).getClass.getCanonicalName) // scala.collection.immutable.$colon$colon
println(Array[Int](1).getClass == Array[Double](1.0).getClass) // False
println(Array[Int](1).getClass.getCanonicalName) // int[]
我现在想知道有什么方法可以做到这一点吗?
【问题讨论】:
-
看看 Scala 中的类型擦除:squidarth.com/scala/types/2019/01/11/type-erasure-scala.html
-
您要解决的元问题是什么?
-
上一个 OP 的问题在这里:stackoverflow.com/questions/64002546/…
标签: scala generics reflection scala-reflect erasure