title: Dynamically decomposing and assembling tuples
body: <p>I have some code that involves verifying the existence of keys/values in a user-generated Map, which is resulting in many levels of nested code. TypeSafe's Slick framework has a really clean way of loading a database result into a list of tuples ex:</p>
  
  <pre><code>Q.queryNA[Tuple3[Int,String,Double]]("SELECT post_id,post_title,post_score FROM posts").list
  </code></pre>
  
  <p>I was thinking this syntax could be a clean way to verify and pattern match multiple fields in one go ex:</p>
  
  <pre><code>parseMap[Tuple3[Int,String,Double]](map_in,"post_id","post_title","post_score") match {
      ((post_id,post_title,post_score)) =&gt; //...do processing
      _ =&gt; println("one or more fields were missing or could not be parsed to datatype")
  }
  </code></pre>
  
  <p>I've been trying to work through this problem, but I may be in over my head. My <em>pseudocode</em> might look something like this (for now lets assume the map will validate to keep the code simple)</p>
  
  <pre><code>def parseMap[T&lt;:Product](map_in: Map[Any],fields: String*) = {
       val head = Tuple1(map_in(fields.head).as[classOf[T._1]])
       if(T.isInstanceOf[Tuple1[_]]) head else head ++ parseMap[T(1-&gt;arity)](map_in,fields.tail)
  }
  </code></pre>
  
  <p>So, obviously this code won't come close to compiling. The main issues being
  1. T is the template type, not a value, so classOf will not work with it.
  2. decoupling the remaining elements of the Tuple (and their datatypes), then re-concatenating them.</p>
  
  <p>It seems like there is a lot of stuff going on here that isn't possible to do, but since the Slick framework does it there must be a way. I tried reading their github code for the queryNA method, but it was beyond my level of expertise.</p>
  
  <p>Another syntax I've considered, though not the same as the Slick syntax is to write a def that returns a Product object, and takes in a list of field name and datatype tuples ex.</p>
  
  <pre><code>List("post_id"-&gt;Int,"post_title"-&gt;String,"post_score"-&gt;Double)
  </code></pre>
  
  <p>Though that still doesn't solve my underlying problem of having to dynamically assemble a tuple.</p>