I would like to create a Map type so something like the below is possible:
VariantMap(1) = "Test"
VariantMap("a") = 42
and VariantMap("a")
would have a type of Option[Int]
. Here is the code I have so far which results in Option[Nothing]
:
object VariantMap {
import scala.reflect.Manifest
private var _map= Map.empty[Any,(Manifest[_], Any)]
def update[T](name: Any, item: T)(implicit m: Manifest[T]) {
_map = _map(name) = (m, item)
}
def apply[T](key:Any)(implicit m : Manifest[T]) = {
val o = _map.get(key)
o match {
case Some((om: Manifest[_], s : Any)) => Some[T](s.asInstanceOf[T])
case _ => None
}
}
}
I'm new to scala so I apologize if I am missing something obvious.