title: Data sharing between Akka actors
body: <p>Based on this question <a href="https://stackoverflow.com/questions/52006036/data-sharing-in-akka">Data sharing in Akka</a> I've implemented a simple akka hierarchy to share data between two actors: </p>
  
  <pre><code>package playground
  
  import akka.actor.{Actor, ActorRef, ActorSystem, Props}
  
  case object GetFromDb
  case object GetList
  case object ProcessList
  
  class ParentActor extends Actor {
    var fromDb: List[Int] = List()
  
    def receive = {
      case GetFromDb =&gt;
        // query the database and replace fromDb with a new list
        fromDb = List(1,2,3)
  
      case GetList =&gt;
        sender() ! fromDb
    }
  }
  
  class ChildActor(parent: ActorRef) extends Actor {
    def receive = {
      case ProcessList =&gt;
        // get the list from the parent
        parent ! GetList
      case fromDb: List[Int] =&gt;
        fromDb.foreach(println)
    }
  }
  
  object AkkaDataSharingDriver extends App {
  
    val system: ActorSystem = ActorSystem("as")
    implicit val ec = system.dispatcher
  
    val parentActorRef: ActorRef = system.actorOf(Props(new ParentActor()))
    val childActorRef: ActorRef = system.actorOf(Props(new ChildActor(parentActorRef)))
  
    parentActorRef ! GetFromDb
    parentActorRef ! GetList
  
    childActorRef ! ProcessList
  
  }
  </code></pre>
  
  <p>The List values 1,2,3 are printed so it appears the message <code>fromDb</code> is sent to <code>ChildActor</code> causing this <code>foreach</code> to be invoked : </p>
  
  <pre><code>case fromDb: List[Int] =&gt;
        fromDb.foreach(println)
  </code></pre>
  
  <p>But as the message with <code>fromDb</code> is never explicitly sent what is causing </p>
  
  <pre><code>fromDb.foreach(println)
  </code></pre>
  
  <p>to be invoked ? Is it somehow sent implicitly ?</p>