How to overload bang(!) operator in Scala Actor model?

后端 未结 3 1938
终归单人心
终归单人心 2021-01-20 02:56

In an Actor model implementation in Scala, can we override the bang(!) operator. Can we modify the operation of message passing by overloading this operator?

Example

3条回答
  •  日久生厌
    2021-01-20 03:34

    In Akka you cannot actually override the ! operator, since you cannot create subclasses of ActorRef in a meaningful way (i.e. they would not be generated by the respective factory methods), and the reason for this is that it is not actually what you want (please trust me here).

    Your stated use-case is already covered by built-in functionality:

    import akka.event.LoggingReceive
    def receive = LoggingReceive {
      case x => ...
    }
    

    which will log a message for each invocation if you enable these configuration settings:

    akka {
      loglevel = DEBUG
      actor.debug {
        receive = on     // this enables the above
        autoreceive = on // same for the likes of PoisonPill, Kill, …
        lifecycle = on   // will log actor creation, restart, termination
      }
    }
    

提交回复
热议问题