I have a class with a generic type parameter, and I want to get a reference to the constructor for the generic type.
This only works without erasure warnings if I import the entirety of scala.reflect.runtime.universe._, instead of importing into an alias (e.g. "ru").
It is not clear to me what the cause is. I suspected an implicit must be being brought into scope from universe, but haven't found anything relevant via IntelliJ's implicit tools.
Can anyone tell me why this happens, and if it is an implicit, which one?
This compiles without warnings:
import scala.reflect.runtime.universe._
class CaseClassesExample[T :TypeTag] {
def doWork: Unit = {
val tpe = weakTypeTag[T].tpe
val ctor = tpe.decls.collectFirst {
case m: MethodSymbol if m.isPrimaryConstructor => m
}
}
}
This compiles with an erasure warning for MethodSymbol:
import scala.reflect.runtime.{universe => ru}
class CaseClassesExample[T : ru.TypeTag] {
def doWork: Unit = {
val tpe = ru.weakTypeTag[T].tpe
val ctor = tpe.decls.collectFirst {
case m: ru.MethodSymbol if m.isPrimaryConstructor => m
}
}
}
Warning:
[warn] abstract type pattern reflect.runtime.universe.MethodSymbol is unchecked since it is eliminated by erasure
[warn] case m: ru.MethodSymbol => m.isConstructor && m.isPrimaryConstructor
[warn] ^
Split Imports:
To narrow down, I have split the imports as follows - interestingly, IntelliJ states the second import is unused, but removing it brings back the warnings:
import scala.reflect.runtime.{universe => ru}
class CaseClassesExample[T : ru.TypeTag] {
def doWork: Unit = {
val tpe = ru.weakTypeTag[T].tpe
import scala.reflect.runtime.universe._
//IntelliJ highlights above import as unused - but removing it brings warning back
val ctor = tpe.decls.collectFirst {
case m: ru.MethodSymbol if m.isPrimaryConstructor => m
}
}
}
Aucun commentaire:
Enregistrer un commentaire