This is essentially my question, but the accepted answer stops at returning their symbols rather than the case objects themselves.
In theory, this should be easy:
def getCaseObjects(enumType: Type) = {
val m = ru.runtimeMirror(getClass.getClassLoader)
enumType.typeSymbol.asClass.knownDirectSubclasses map { subclass =>
val enumObjMirror = m.reflectClass(subclass.asClass.toType.typeSymbol.asClass)
enumObjMirror.reflectConstructor(subclass.asClass.toType.decl(ru.termNames.CONSTRUCTOR).asMethod)()
}
}
And this works!
...Except that they are entirely new instances compared to the ones contained in their Parent
sealed trait; hooray, I've busted the "case objects are singleton" assumption!
I could override equals
and hashCode
in my Parent
sealed trait and be done with it, but I'd prefer a way to get those particular case objects rather than ones that happen to look like them. Is this possible? I'm on 2.11 if that makes any difference.