1

Can I get a List of all the case objects that implement a sealed trait? For example.,

I have a sealed trait as below:

sealed trait MyTrait
case object A extends MyTrait
case object B extends MyTrait

I want to now have something that would give me the List of all the objects that implement this sealed trait?

joesan
  • 13,963
  • 27
  • 95
  • 232
  • 3
    Yes, you can. [Macros FTW](http://stackoverflow.com/a/13672520/298389) – om-nom-nom Jul 29 '14 at 16:25
  • there shouldn't be anything preventing it, all "sealed" does is that you can't inherit outside the document it's in. This doesn't appy to the sub-types – Electric Coffee Jul 29 '14 at 20:59
  • as a related question, how does the Scala compiler know all sub-classes of a trait since it can provide `non-exhaustive match` warnings? – Kevin Meredith Jul 30 '14 at 02:32
  • 1
    That exactly is the purpose of the keyword sealed. It is a hint to the compiler that all the sub classes of the trait are in the same package. – joesan Jul 30 '14 at 07:55

0 Answers0