2017-06-21 32 views
0

我想通过以下方式运行Akka Actorreceive method阿卡接收方法的情况下mutable.Map给出了一个运行时错误

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]]=>

是否有其他办法,我可以通过一个mutable map具有value作为另一个mutable map?任何帮助表示赞赏。

+2

演员与可变消息的沟通是一个坏主意,正如[这里]所述(http://doc.akka.io/docs/akka/current/scala/actors.html#messages-and-immutability)和[here ](http://doc.akka.io/docs/akka/current/scala/general/jmm.html#jmm-shared-state)。 – chunjef

+0

@chunjef我可以直观地看到为什么这是一个坏主意,除了我只需要一个演员的可变性功能。然后传递给另一个演员进行阅读,不再应用更改。 – Zzrot

回答

1

一提的是该语句(如果它的工作)会匹配任何mutable.Map由于type erasure是很重要的:

[...] 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.

要解决这个问题,你可以简单地创建一个包装类携带地图:

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

,然后在接收方法:

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

无论您想要传递的类型如何,通常都可以使用case class作为包装,并且仅为消息提供更有意义的名称。

关于错误,很难说没有更多的代码,但你应该绕过这个方法反正。

+0

这工作完美!谢谢。 – Zzrot

相关问题