As it turns out, it is possible to define a heterogeneous map in Scala. Here's a rough sketch:
class HMap[A[_], B[_]] extends Iterable[HMap.Mapping[A, B, _]] {
private val self = mutable.Map[A[_], B[_]]()
def toMapping[T](a: A[_], b: B[_]): HMap.Mapping[A, B, T] = {
HMap.Mapping(a.asInstanceOf[A[T]], b.asInstanceOf[B[T]])
}
def iterator: Iterator[HMap.Mapping[A, B, _]] =
new Iterator[HMap.Mapping[A, B, _]] {
val sub = self.iterator
def hasNext = sub.hasNext
def next(): HMap.Mapping[A, B, _] = {
val (key, value) = sub.next()
toMapping(key, value)
}
}
def update[T](key: A[T], value: B[T]) = (self(key) = value)
def get[T](key: A[T]) = self.get(key).asInstanceOf[Option[B[T]]]
def apply[T](key: A[T]) = self(key).asInstanceOf[B[T]]
}
object HMap {
case class Mapping[A[_], B[_], T](val key: A[T], val value: B[T])
}
This could be made completely typesafe by internally using a linked list of mappings instead of a map, but this is better for performance.
My original example would look like this:
object Example {
type Identity[T] = T
type Handler[T] = (T) => _
val handlers = new HMap[Identity, Handler]
def doSomething() {
for (HMap.Mapping(value, handler) <- handlers) {
handler(value)
}
}
}
This is almost perfect, except I'm not sure how to add bounds.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…