title: How can I combine fluent interfaces with a functional style in Scala?
body: <p>I've been reading about the OO 'fluent interface' approach in <a href="https://stackoverflow.com/questions/4625580/what-is-the-scala-equivalent-to-a-java-builder-pattern">Java</a>, <a href="https://github.com/raganwald/Katy" rel="nofollow noreferrer">JavaScript</a> and <a href="http://scalada.blogspot.com/2008/02/thistype-for-chaining-method-calls.html" rel="nofollow noreferrer">Scala</a> and I like the look of it, but have been struggling to see how to reconcile it with a more type-based/functional approach in Scala.</p>
  
  <p>To give a very specific example of what I mean: I've written an API client which can be invoked like this:</p>
  
  <pre><code>val response = MyTargetApi.get("orders", 24)
  </code></pre>
  
  <p>The return value from <code>get()</code> is a <code>Tuple3</code> type called <code>RestfulResponse</code>, as defined in my <a href="https://stackoverflow.com/questions/7441277/scala-type-keyword-how-best-to-use-it-across-multiple-classes">package object</a>:</p>
  
  <pre><code>// 1. Return code
  // 2. Response headers
  // 2. Response body (Option)
  type RestfulResponse = (Int, List[String], Option[String])
  </code></pre>
  
  <p>This works fine - and I don't really want to sacrifice the functional simplicity of a tuple return value - but I would like to extend the library with various 'fluent' method calls, perhaps something like this:</p>
  
  <pre><code>val response = MyTargetApi.get("customers", 55).throwIfError()
  // Or perhaps:
  MyTargetApi.get("orders", 24).debugPrint(verbose=true)
  </code></pre>
  
  <p>How can I combine the functional simplicity of <code>get()</code> returning a typed tuple (or similar) with the ability to add more 'fluent' capabilities to my API?</p>