title: scala implicit extracted values in pattern matching?
body: <p>We often need to pass through the code context information like the user that is performing the action. We use this context for various things like authorization checks. In these cases implicit values can prove to be very useful to reduce boiler plate code. </p>
  
  <p>Let's say we have a simple execution context that we pass around: <code>case class EC(initiatingUser:User)</code></p>
  
  <p>We can have handy guards:</p>
  
  <pre><code>def onlyAdmins(f: =&gt; T)(implicit context:EC) = context match{
    case EC(u) if(u.roles.contain(Role.ADMIN)) =&gt; f
    case _ =&gt; throw new UnauthorizedException("Only admins can perform this action")
  }
  
  val result = onlyAdmins{
    //do something adminy 
  }
  </code></pre>
  
  <p>I recently found myself in need to do this when working with Akka actors but they make use of pattern matching and I am yet to find a good way of making implicits work well with extractors.</p>
  
  <p>First you would need to pass the context with every command, but that's easy:</p>
  
  <pre><code>case class DeleteCommand(entityId:Long)(implicit executionContext:EC)
  //note that you need to overwrite unapply to extract that context
  </code></pre>
  
  <p>But the receive function looks like this:</p>
  
  <pre><code>class MyActor extends Actor{
    def receive = {
       case DeleteCommand(entityId, context) =&gt; {
         implicit val c = context
         sender ! onlyAdmins{
           //do something adminy that also uses context
         }
       }
    }
  }
  </code></pre>
  
  <p>It would be much simpler if extracted variables could be marked as implicit but I haven't seen this feature:</p>
  
  <pre><code>def receive = {
    case DeleteCommand(entityId, implicit context) =&gt; sender ! onlyAdmins{
      //do something adminy (that also uses context)
    }
  }
  </code></pre>
  
  <p>Are you aware of any alternative ways of coding this so it reduces the boilerplate code? </p>