I recently found that the scala reflection library seems to be out of maintenance and interact poorly with a few other features, e.g. For the most simple use case, when the types of variables needs to be extracted, scala reflection can't get it right:
class InferTypeTag extends AnyFunSpec {
import InferTypeTag._
it("error case") {
val x1: String = "A"
val x2: x1.type = x1
type T3 = x1.type
val x3: T3 = x1
val t1 = inferWeak(x1)
val t2 = inferWeak(x2)
val t3 = inferWeak(x3)
println(
s"$t1\n$t2\n$t3"
)
}
}
object InferTypeTag {
def inferWeak[T](v: T)(implicit ev: WeakTypeTag[T]) = ev
}
Here, you would imagine that x2 and x3 are declared to be of type x1.type
, their compile-time type inspected from WeakTypeTag should both be different from that of x1, which is a String type variable, a super type of x1.type
. And type of x2 and x3 should be at least equal (as defined in =:=).
Lord how it is far from reality! When running the above test, the 3 types are actually:
TypeTag[String]
TypeTag[String]
WeakTypeTag[T3]
So x1 and x2 have the same declared type? And x3 can't have a concrete type at compile-time? What is going here?