title: Scala pass function as argument and execute conditionally
body: <p>Say  I have an object with a method called <code>protect</code>.</p>
  
  <p>This is supposed to take a function with <code>JValue =&gt; JValue</code>(non generic parameters).</p>
  
  <pre><code> def protect(func: (JValue) =&gt; JValue) = {
          if (Something.is) {
              InMemoryResponse(Array(), Nil, Nil, 200)
          } else {
              InMemoryResponse(Array(), Nil, Nil, 401)
          }
    }
  </code></pre>
  
  <p>How can I now call the function passing its original argument?</p>
  
  <p>The goal is to check if a user session exists before responding to a REST request. If it doesn't then a <code>401 Forbidden</code> will be returned automatically, so something like the following. In this case I want to pass <code>Users.getUsers</code> as the argument function and limit as the param to the function.</p>
  
  <p>So inside <code>protect</code>, if the <code>Session</code> exists the function will execute and display.</p>
  
  <pre><code>serve {
      case "user" :: limit :: Nil JsonGet _ =&gt;
          AuthorizedReq.protect(json =&gt; Users.getUsers(limit))
  </code></pre>