A new comer to Scala, but familiar with C++ I am attempting to implement (possibly misguidedly) a little library on the top from the sqlite4java library to let me auto-fill tuples of abritrary type from query rows (rows whose column types are each suitable for the particular tuple element type).

In C++ I normally implement this using boost::tuples and compile-time template recursion (ended using template expertise as proven below). Boost tuples are implemented very much like Haskell HLists. The pattern could be (presuming, for simplicity the totally came back as vector of strings):

template<typename T1, typename T2>
void populateTuple( boost::tuples::cons<T1, T2>& tupleRec, int index, const std::vector<std::string>& vals )
{
    tupleRec.head = boost::lexical_cast<T1>( vals[index] );
    populateTuple( tupleRec.tail, index+1, vals );
}

template<typename T>
void populateTuple( boost::tuples::cons<T, boost::tuples::null_type>& tupleRec, int index, const std::vector<std::string>& vals )
{
    tupleRec.head = boost::lexical_cast<T>( vals[index] );
}

(Apologies - I have not run the above mentioned via a compiler, but am wishing it shows what i'm saying)

I'd like to have the ability to make a move similar with Scala. I'm able to recurse on the general Tuple type through the Product trait - and obtain the kind of each element at run time using pattern matching (for that few column types I support). However haven't found a method to assign Tuple elements through the product trait. And to tell the truth, I am not believing that this can be a particularly nice or idiomatic method of doing things i require anyway.

But something similar to:

val returnedFromQuery = List[String]( "Hello", "4", "6.0" )
val rowAsTuples = interpretListAsTuple[(String, Int, Float)]( returnedFromQuery )

Where rowAsTuples has type (String, Int, Float). Again, please excuse any syntax errors.

Anybody have ideas? Or alternative suggestions? Ahead of time - I am not thinking about any greater-level SQL query libraries. I am pleased with sqlite4java but wish to wrap it having a simple more abstract interface.

I believe you should attempt using pattern-matching rather than interpretation. First, we want something which will take out our types from strings using unapply:

object Stringy {
  def unapply(s: String) = Some(s)
}
object Inty {
  def unapply(s: String) = {
    try { Some(s.toInt) }
    catch { case nfe: NumberFormatException => None }
  }
}
object Floaty {
  def unapply(s: String) = {
    try { Some(s.toFloat) }
    catch { case nfe: NumberFormatException => None }
  }
}

Now we are able to rely on them in pattern matches:

scala> List("Hello","4","6.0") match {
  case Stringy(s) :: Inty(i) :: Floaty(f) :: Nil => Some((s,i,f))
  case _ => None
}
res3: Option[(String, Int, Float)] = Some((Hello,4,6.0))

Observe that should you choose it by doing this, you (1) do not have to return tuples if you won't want to (2) get access to all of the parsed out variables immediately (3) have automatic error checking built-in (with options).

Try HList in the MetaScala library.