mardi 23 octobre 2018

Using Scala reflection, how can I determine if a type is a case object?

sealed trait Weekday
case object Monday extends Weekday
case object Tuesday extends Weekday
case object Wednesday extends Weekday
case class SampleEnum2(e1: Weekday, e2: Weekday)

If I reflect on e1 I can know learn that Weekday a sealed trait: (where tt is TypeTag[T])

tt.tpe.typeSymbol.asClass.isTrait
tt.tpe.typeSymbol.asClass.isSealed

So now I want to know something about Weekdays, specifically if they're case objects (vs case classes or something else):

val subs = tt.tpe.typeSymbol.asClass.knownDirectSubclasses.toList

This will give me Monday, Tuesday, Wednesday. If I look at those, get their typeSymbol.asClass, I don't see a comparable isObject or something like that.

How can I know if the elements of subs are case objects?





Aucun commentaire:

Enregistrer un commentaire