You can use ClassManifest
(ClassTag
in Scala 2.10) to improve your map solution:
package object features {
type FeatureMap = Map[Class[_ <: Feature], Int]
}
abstract class Config(val param: Int) {
def ConfigParameter: Int
def featureMap: FeatureMap
def featureCount[T<:Feature]( implicit man: ClassManifest[T] ): Int =
featureMap( man.erasure )
}
object Config {
def makeBasic(param: Int) = new Config(param) {
val ConfigParameter = param
lazy val featureMap: FeatureMap = Map(
classOf[Foo] -> 3,
classOf[Bar] -> 7
)
}
}
Each time you call featureCount
the compiler will use the right classManifest for the type you passed between brackets. The erasure
method returns the coresponding class.
Remark: Avoid abstract vals, it has annoying effects and can break binary compatibility.