title: How to `andThen` Akka `Receive`s?
body: <p>I have the following:</p>
  
  <pre><code>def become(r: ListMap[String, Receive]) {
    val undefined: PartialFunction[Any, Unit] = Map.empty
  
    val receive = r.foldLeft(undefined) { (accum: Receive, rhs: Pair[String, Receive]) =&gt;
      accum orElse rhs._2
    }
  
    val echo: Receive = {
      case message =&gt; println(s"*************************************************** ${sender}: processing ${message}")
    }
  
    context.become(echo andThen receive)
  }
  </code></pre>
  
  <p><code>receive</code> by itself works fine, but when <code>echo andThen receive</code> is used, the system spits out a bunch of:</p>
  
  <pre><code>scala.MatchError: () (of class scala.runtime.BoxedUnit)
      at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:248)
      at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:246)
      at kvstore.Replica$$anonfun$secondary$1.applyOrElse(Replica.scala:234)
      at scala.runtime.AbstractPartialFunction$mcVL$sp.apply$mcVL$sp(AbstractPartialFunction.scala:33)
      at scala.runtime.AbstractPartialFunction$mcVL$sp.apply(AbstractPartialFunction.scala:33)
      at scala.runtime.AbstractPartialFunction$mcVL$sp.apply(AbstractPartialFunction.scala:25)
      at kvstore.Replica$$anonfun$primary$1.applyOrElse(Replica.scala:148)
      at scala.PartialFunction$OrElse.apply(PartialFunction.scala:162)
      at scala.PartialFunction$class.applyOrElse(PartialFunction.scala:118)
      at scala.collection.AbstractMap.applyOrElse(Map.scala:58)
      at scala.PartialFunction$OrElse.apply(PartialFunction.scala:162)
      at scala.PartialFunction$AndThen.applyOrElse(PartialFunction.scala:185)
      at akka.actor.ActorCell.receiveMessage(ActorCell.scala:498)
      at akka.actor.ActorCell.invoke(ActorCell.scala:456)
      at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:237)
      at akka.dispatch.Mailbox.run(Mailbox.scala:219)
      at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
      at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
      at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
      at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
      at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
  </code></pre>
  
  <p>The stacktrace points to:</p>
  
  <pre><code>def secondary(
                  // expected sequence id
                  expectedSeq: Long,
                  // a map from sequence id to replication requester
                  replicationRequester: Map[Long, ActorRef],
                  // a map from key to triple of sequence id, persistence repeat, and persistence timeout
                  persistenceSchedulers: Map[String, Triple[Long, Cancellable, Cancellable]]): Receive = {
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>def primary(
                // a map from secondary replicas to replicators
                replicators: Map[ActorRef, ActorRef],
                // a map from pair of key and replicator to message id, replication repeat, and replication timeout
                replicationSchedulers: Map[Pair[String, ActorRef], Triple[Long, Cancellable, Cancellable]],
                // map from message id to pair of replication requester and set of replicas
                awaitingReplication: Map[Long, Pair[ActorRef, Set[ActorRef]]]): Receive = {
  </code></pre>
  
  <p>both of which are used through something like:</p>
  
  <pre><code>def receive = {
    case JoinedSecondary =&gt; {
      receivePartialFunctions = ListMap(secondaryReceiveName -&gt; secondary(0, Map.empty, Map.empty))
      become(receivePartialFunctions)
    }
  
    case JoinedPrimary =&gt; {
      val replicator = context.actorOf(Props(classOf[Replicator], self), s"replicator-${System.nanoTime()}")
  
      receivePartialFunctions = ListMap(
        primaryReceiveName -&gt; primary(Map(self -&gt; replicator), Map.empty, Map.empty),
        secondaryReceiveName -&gt; secondary(0, Map.empty, Map.empty)
      )
      become(receivePartialFunctions)
    }
  }
  </code></pre>
  
  <p>What does this exception mean and what needs to be done to fix it?</p>