Option [Object]上的Scala匹配并非详尽无遗,Akka接收函数中的Some(o)和None

gsi*_*ard 1 multithreading jvm scala actor akka

对于下面选项[实体]的匹配,第三种情况需要详尽无遗.为什么?

entitiesMap是一个包含不可变Map [UUID,Entity]var.它在一个Akka Actor中被访问和更新,以避免线程问题.以下是接收函数的摘录:

class WorldActor extends Actor {

  var world: World = null

  override def receive = {

    case WorldSet(w) =>
      world = w

    // get that state and apply it to its entity
    case s: State if (world != null) =>
      val uuid = new UUID(s.getUuid().getL1(), s.getUuid().getL2())
      world.entitiesMap.get(uuid) match {
        case Some(ent) =>
          // Update entity's state
          ent.setEntityState(s)
        case None =>
          Log.error("World doesn't contain entity uuid: " + uuid)
        case other =>
          Log.error("Received unknown message: " + other)
      }

    case AddEntity(ent) if (world != null && ent != null) =>
      if (!world.entitiesMap.contains(ent.uuid))
        world.entitiesMap += ent.uuid -> ent

    case RemoveEntity(ent) if (world != null && ent != null) =>
      if (world.entitiesMap.contains(ent.uuid))
        world.entitiesMap -= ent.uuid

    case other => // ignore
  }

}

class World {
  // Entity container
  var entitiesMap = Map[UUID,Entity]()
}
Run Code Online (Sandbox Code Playgroud)

上述代码不时会报告:

Received unknown message: None
Run Code Online (Sandbox Code Playgroud)

为什么不是这种情况只是上面没有图案抓住它?

编辑

我发现,当消息出现的bug 国家时逢消息之前AddEntity,即当entitiesMap还没有包含在实体通过消息refered给国家.

01:52 ERROR: [State] Received unknown message: None
uuid: 1b234d30-92ae-11e3-aa12-7071bcb09717
Thread: 152

01:52 ERROR: [State] Received unknown message: None
uuid: 1b234d30-92ae-11e3-aa12-7071bcb09717
Thread: 32

01:52  INFO: [AddEntity] 1b234d30-92ae-11e3-aa12-7071bcb09717: Cube@2f9c3beb
Thread: 152
Run Code Online (Sandbox Code Playgroud)

毕竟这可能是一个线程问题吗?在演员里面?

编辑2

根据下面的海报建议,我已经记录了其他的类名,类加载器引用和类文件路径.他们都是一样的.

编辑3

其他==无假的

other.eq(None)false

other.equals(None)false

other.hashCode == None.hashCode

System.identityHashCode(other)!= System.identityHashCode(None)

som*_*ytt 6

这听起来像一个类加载器/类路径问题,其中None$两个类加载器加载,而两个无$ .Module $不比较相等.

由于Map是不可变的,因此在添加之后,您已经创建了一个新的Map.假设外部加载器加载了新Map的类; 然后当该地图返回None时,它将返回外星人None.

请注意,当您添加到非常小的地图时,它会使用new来创建下一个更大的地图; 但经过一些这样的分配后,它会切换到HashMap.因此竞争类加载器可能会改变您获得的地图的明显行为.

我不仅打印出类名,还打印出类加载器以及加载类的位置.