There are lots of ways to do this, but I'd suggest using macros (compile-time reflection) over runtime reflection, for the sake of both performance and (more importantly) type safety.
Here's a quick implementation with macros:
import scala.language.experimental.macros
object MacroUtils {
import scala.reflect.macros.Context
def values = macro MacroUtils.values_impl
def values_impl(c: Context) = {
import c.universe._
val objs = c.enclosingClass.collect {
case ModuleDef(mods, name, _) if mods hasFlag Flag.CASE => Ident(name)
}
c.Expr[List[Any]](
Apply(Select(reify(List).tree, newTermName("apply")), objs.toList)
)
}
}
trait Population
object Units {
val values = MacroUtils.values
case object CITIZEN extends Population
case object WORKER extends Population
}
And then, for example:
scala> val populations: List[Population] = Units.values
populations: List[Population] = List(CITIZEN, WORKER)
Note that the compiler knows that the list of case objects can be statically typed as a list of populations.