I'm new to scala and I need a structure where I can easily add new indivdual accessable constants and also iterate over all of them. Something like this:
object Constants {
val A = Something(...)
val B = Something(...)
val C = Something(...)
// ...
val listOfConstants = (A, B, C, ...)
}
But without the need to add the constant to the list manually. I considered a Map but this would make it harder to access the constants and I ended up writing this:
def getElements[T : ClassTag](c : Any) : Set[T] = {
var result : Set[T] = Set()
for (field <- c.getClass.getDeclaredFields) {
field.setAccessible(true)
field.get(c) match {
case t: T => result += t
case _ =>
}
}
result
}
and then using the following line for structures like this
val listOfConstants = getElements[Something](Constants)
Is this a dirty solution? Are there better alternatives?