Akka Receive Method case mutable.Map 给出运行时错误

Akka Receive Method case mutable.Map gives a runtime error

我正尝试通过以下方式 运行 Akka Actorreceive method

  def receive = {
    case  x: collection.mutable.Map[String, collection.mutable.Map[String,Float]]=>
    insertValueIntoTable(x)
  }

我可以毫无问题地编译它,但我收到错误:

Error:(83, 57) ']' expected but '.' found. case x: collection.mutable.Map[String, collection.mutable.Map[String,Float]]=>

是否有任何其他方法可以将具有 valuemutable map 作为另一个 mutable map 传递?感谢任何帮助。

值得一提的是,由于 type erasure:

,此语句(如果有效)将匹配任何 mutable.Map

[...] To implement generics, the Java compiler applies type erasure to:

  • Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
  • [...]

Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.

要解决这个问题,您可以简单地创建一个包含您的地图的包装器 class:

case class MapMessage(map: mutable.Map[String, mutable.Map[String,Float]])

然后在你的接收方法中:

def receive = {
    case  MapMessage(x)=>
        insertValueIntoTable(x)
}

无论您想传递哪种类型,使用 case classes 作为包装器通常都很好,如果只是为了给消息起一个更有意义的名称。

关于这个错误,如果没有更多的代码很难判断,但无论如何你应该用这个方法来解决它。