score:0

I am tempted to say, that even if there is a solution to your question, there's something which does not sound right in your design. On one hand, you use complex types which are nice for type safety and inference at compile time. On the other hand you use a map to store runtime information. So either, you simplify your traits and forget about type-saftey, or you forget about storing types in maps at runtime.

For example, if you want to associate a writer to a particular MyData subclass, you could use type classes, which are flexible and resolved at compile-time.

score:0

Does this is what you want?

trait MyData

trait MyId

trait Writer[T <: MyData] {
  def save(data: T)
}

var writers: Map[Manifest[_ <: MyData], Writer[MyData]] = Map.empty

var data: Map[MyId, (MyData, Manifest[_ <: MyData])] = Map.empty

data.foreach {
  case (id, (d, m)) =>
    writers.get(m).map(_.save(d)) // [1]
}

def addData[T <: MyData](id: MyId, d: T)(implicit m: Manifest[T]) = {
  data += ((id, (d, m)))
}
// you don't need to give the m parameter, it is given by the compiler
addData(new MyId {}, new MyData {})
  1. you surely want a better search logic here

Related Query

More Query from same tag