mercredi 23 septembre 2020

How to distinguish parameteric types?

I am confused about subtypes in Scala. My major question is how to distinguish C[T1] from C[T2]. There are two scenarios:

  1. C[T1] "equals" C[T2] because they are all subtypes of C.
  2. C[T1] does not "equal" C[T2] because C[T1] and C[T2] are different types eventually.

I have tried some ways like .getClass, seems that this strategy won't work because we have primitive types.

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[]

I now wonder is there some way I can do this?





Aucun commentaire:

Enregistrer un commentaire