lundi 25 novembre 2019

In Scala, is the reflection module ill suited for dealing with runtime, possibly erased types?

Considering a simple use case to extract fields of an object in Java reflection:

val fields = this.getClass.getDeclaredFields

it may return incomplete information if generic type is used, but it is short, fast and can handle most cases.

In Scala reflection module (scala.reflect), it appears that such one-liner doesn't exist, the shortest code I know of that can successfully handle this case is:

trait Thing[T <: Thing[T]{

implicit ev: TypeTag[T]

scala.reflect.runtime.universe.typeOf[T].members
}

This uses a redundant F-bounded polymorphism and a TypeTag doesn't carry extra information comparing to a Java class, is there any way to make it shorter, at least as short as Java (which is a very verbose language)?

Thanks a lot for your advice





Aucun commentaire:

Enregistrer un commentaire