Take the 2-minute tour ×

Given a Tuple type

type T = (String, Int, String)

Is there any way I can get a type T1, where T1 would be

type T1 = (MyClass, String, Int, String)

I would love to be able to declare a class like

class TupleTypes[T] extends AnotherClass[T1]

Note: The tuple size is not known and

type T1 = (MyClass, T)

will not return what I want, it will return (MyClass, (String, Int, String)) which is different.


share|improve this question

3 Answers 3

late to the party, but in the event that you are seeking a "better" solution in regard to the ScalaQuery problem, try this:

1) create mapper base class with ID

import org.scalaquery.ql.extended.{ExtendedTable => Table}

abstract class Mapper[T](table: String) extends Table[T](None, table) {
  def id = column[Int]("id", O PrimaryKey)

2) extend mapper base using case class/companion object (i.e. not tuple based)

case class Foo (bar: String)
object Foos extends _Mapper[Foo]("foo") {
  def foo = column[String]("foo")

then you can do something like:

def show: List[Foo] = {
  val q = (for { f <- Foos } yield f)

  val foos = db withSession {
    foos.list map { case t:T => t }

and have a navigable object to work with (vs. index-based tuple).

Now, sometimes you don't want an enormous object graph when you only need a subset of fields from a group of entities.

That's where projections come in, just create a case class that represents the set of fields that you want and, voila, a navigable projection object to work with:

case class Yum (foo: String, baz: String)

def show: List[Yum] = {
  val q = (for { f <- Foos; b <- Bars; if f.id is b.fooID } yield (f.foo, b.baz))

  val yums = db withSession {
    yums.list map { case t:T => t }

Fairly simple, should be encapsulated in a cake driven DAO, but the general principle is: take the case class/object route.

Should note that ScalaQuery kicks unbelievable azz, Zeiger is brilliant! (as are many in the Scala community, future is looking bright on Scala way ;-))

share|improve this answer

You can do this using the HList to tuple conversions from shapeless.

scala> import shapeless._ ; import Tuples._
import shapeless._
import Tuples._

scala> class MyClass ; val m = new MyClass
defined class MyClass
m: MyClass = MyClass@3859023a

scala> val t1 = ("foo", 23, "bar")
t1: (String, Int, String) = (foo,23,bar)

scala> val t2 = (m :: t1.hlisted) tupled
t2: (MyClass, String, Int, String) = (MyClass@3859023a,foo,23,bar)
share|improve this answer
Thanks Miles, very interesting. The question is how do I get the type of t2 and use it for a class definition? Like in the example above class TupleTypes[T] extends AnotherClass[T1] is there anyway to get T1? –  mericano1 Mar 31 '12 at 16:25
Could you give me some examples of the general shapes of the members you plan to add to TupleTypes and AnotherClass. And could you also say if you need there to be any sort of useful relationship between T and T1 (ie. do you need to be able to construct a value of type T1 given values of types MyClass and T and/or vice versa)? –  Miles Sabin Mar 31 '12 at 22:20
Hi Miles, I appreciate your help. I'm working with scalaquery and I want to create an abstract base class that has a column of type Long (id). So TupleTypes[T] is actually BaseTable[T]. Other classes extending BaseTable[T] will have a list of columns and the type T will be a tuple like (String, Int, Boolean,Date). Now becasue my BaseClass[T] needs to extend the scalaquery Table and provide the a tuple with all the column types, T1 will have to be (Long, String, Int, Boolean, Date). Hope this explains what I'm trying to achieve. –  mericano1 Apr 1 '12 at 14:01

In my opinion there are no such constructs for tuples, but HList have a behavior very similar to the one you showed. They are considered has advanced type programming structure, and usage can be difficult depending on what you want to achieve. Here are an excellent starter and a nice implementation.

share|improve this answer

Your Answer


