mercredi 24 avril 2019

How do I use Scala reflection to find all subclasses of a trait (without using third-party tools)?

Suppose I have a type, Result:

trait Result[+T] {

  def getValue: T

}

and a subtype of that trait, AnyValResult:

class AnyValResult(value: AnyVal) extends Result[AnyVal] {
  override def getValue: AnyVal = value
}

I want to be able to ask Scala's reflection library for all subtypes of the Result[_] type and have it return a collection that includes AnyValResult.

I've seen lots of people ask this question, and all of them seem to say that they did it using third-party tools. This was a few years ago. Short of reverse-engineering the third-party tools, is there a way to do this that was introduced in more recent versions of Scala? I would prefer not to have to reference some random project on Github if I can do it directly with reflection.





Aucun commentaire:

Enregistrer un commentaire