Huawei-CPLLab / Theater

Actor model framework for Swift
Apache License 2.0
5 stars 1 forks source link

NPE exception: Create a child actor when the current actor is under construction #1

Closed wanghc78 closed 8 years ago

wanghc78 commented 8 years ago

The code will throw the NPE

class B : Actor {...}
class A : Actor {...}
  init() { actorOf(B.init, "B") }
}
system.actorOf(A.init, "A")

The reason is at the current actor A init phase, the actor's ref is not assigned. So it cannot call actorOf() which will use the ref field, to create actor B.

wanghc78 commented 8 years ago

The current fix #d931cfb partially solve the problem. Now user can create child actor in preStart()

But the child actors fields can only be optional var, like

class A : Actor {
  var b:B!
  init() {...}
  preStart() { b = actorOf(B.init, "B") }
}

If we want to support let b:B!, the field assign must still be inside init(), then we still have to pass actorSystem/ref into init(), which causes we can not write arbitrary constructors of an actor.