Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4083368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:18:14+00:00 2026-05-20T18:18:14+00:00

New to Scala, but experienced in C++ I’m trying to implement (possibly misguidedly) a

  • 0

New to Scala, but experienced in C++ I’m trying to implement (possibly misguidedly) a small library on top of the sqlite4java library to allow me to auto-fill tuples of abritrary type from query rows (rows whose column types are each compatible with the respective tuple element type).

In C++ I normally implement this using boost::tuples and compile-time template recursion (terminated using template specialisation as shown below). Boost tuples are implemented very similarly to Haskell HLists. The pattern would be (assuming, for simplicity that the query is returned 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’ve not run the above through a compiler, but am hoping it shows what I mean)

I’d love to be able to do something similar with Scala. I can recurse over a general Tuple type via the Product trait – and get the type of each element at run time using pattern matching (for the small number of column types I support). However I haven’t found a way to assign Tuple elements via the product trait. And to be honest, I’m not convinced that this is a particularly nice or idiomatic way to do what I require anyway.

But something like:

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.

Anyone have any thoughts? Or alternative suggestions? In advance – I’m not interested in any higher-level SQL query libraries. I’m happy with sqlite4java but want to wrap it with a simple more abstract interface.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:18:15+00:00Added an answer on May 20, 2026 at 6:18 pm

    I think you should try using pattern-matching instead of interpretation. First, we need something that will pull 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 can use 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))
    

    Note that if you do it this way, you (1) don’t have to return tuples if you don’t want to; (2) have access to all the parsed out variables right away; (3) have automatic error checking built in (with options).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Scala, but from what I've read it seems the ideal
Now I am confused. I am quite new on Scala, having worked with it
Is there a shorthand for a new line character in Scala? In Java (on
Scala newbie here, I just downloaded Eclipse 3.6.2 and Scala IDE 2.0.0-beta4 (with Scala
I'm new to this concept so I might be wrong in calling it a
I've been working up my answer to Is there a standard Scala function for
I'm trying to scale a container/parent movie clip up so that I effectively zoom
I've learned programming from Java, then tried to learn one programming language per year,
How can i get access token for searching venues in foursquare. From this gem,
I'm creating a series of webservices for my application and i have the need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.