有一个解决方案,但它有点不稳定。(引用下面 Scala 2.10 控制台的输出,作为快速演示)
import scala.reflect.runtime.{universe=>ru}
import scala.collection.mutable
scala> :paste
// Entering paste mode (ctrl-D to finish)
object StackManager {
val stacks = mutable.HashMap[(String, ru.Type), mutable.Stack[_]]()
def get[T:ru.TypeTag](id: String): mutable.Stack[T] = {
stacks.get((id,ru.typeOf[T])) match {
case Some(stack: mutable.Stack[T]) => stack
case _ => {
val newStack = mutable.Stack[T]()
stacks.put((id,ru.typeOf[T]), newStack)
newStack
}
}
}
def set[T:ru.TypeTag](id: String, stack: mutable.Stack[T]) {
stacks.put((id,ru.typeOf[T]), stack)
}
}
// Exiting paste mode, now interpreting.
defined module StackManager
scala> StackManager.set("example", new mutable.Stack[String])
scala> StackManager.set("example", new mutable.Stack[Int])
scala> StackManager.get[Int]("example")
res15: scala.collection.mutable.Stack[Int] = Stack()
scala> StackManager.get[String]("example")
res16: scala.collection.mutable.Stack[String] = Stack()
scala> StackManager.get[Double]("example")
res17: scala.collection.mutable.Stack[Double] = Stack()
不那么时髦的解决方案,没有类型标签:
object StackManager {
val stacks = mutable.HashMap[String, mutable.Stack[_ <:Any]]()
def get[T](id: String): mutable.Stack[T] = {
stacks.get(id) match {
case Some(stack: mutable.Stack[_]) => stack.asInstanceOf[mutable.Stack[T]]
case _ => {
val newStack = mutable.Stack[T]()
stacks.put(id,newStack)
newStack
}
}
}
def set[T](id: String, stack: mutable.Stack[T]) {
stacks.put(id,stack)
}
}
它似乎也进行类型检查:
scala> StackManager.set[String]("string",new mutable.Stack[String])
scala> StackManager.set[Integer]("integer",new mutable.Stack[String])
<console>:11: error: type mismatch;
found : scala.collection.mutable.Stack[String]
required: scala.collection.mutable.Stack[Integer]
StackManager.set[Integer]("integer",new mutable.Stack[String])
scala> StackManager.set[Integer]("integer",new mutable.Stack[Integer])
scala> StackManager.get[Integer]("integer")
res11: scala.collection.mutable.Stack[Integer] = Stack()
不过,我还没有在 scala 2.9 中尝试过。