0

I have the following query in scala:

val query = for {
      (table1, table2) <- Customer leftJoin Transaction on (_.custID === _.custID) 
      if table1.name === "ABCD"          
    } yield (table1.name, table2.date)

When I use query i have to use query._1 or query._2 for name and date respectively. Is the a way to use something of this kind: query.name, query.date

user3098466
  • 329
  • 1
  • 11

2 Answers2

5

Your for expression is returning a tuple, and tuples can be easily deconstructed using pattern matching (nothing Slick-specific here) :

val (foo, bar) = ("foo", "bar") // foo="foo", bar="bar"

You should be able to do the same with the tuple returned by the Slick query :

val (name, date) =
  for {
    (table1, table2) <- Customer leftJoin Transaction on (_.custID === _.custID)
    if table1.name === "ABCD"          
  } yield (table1.name, table2.date)
Cyäegha
  • 4,191
  • 2
  • 20
  • 36
  • The expression is not returning a tuple. It returns a query which will return a list of tuples -> expression.list will return a list of tuples. – user3098466 Jul 29 '15 at 12:10
  • 3
    It makes sense, but then, your exemple of using `query._1` or `query._2` shouldn't work either? That being said, on a list of tuples, you can use something like `myList map { case (name, date) => /* do somehting */}`; or, in your for expression, instead of yielding a tuple, you could yield a case class. I will edit my answer as soon as I have a bit more time. – Cyäegha Jul 29 '15 at 12:24
0

You can use pattern matching for that issue

val (name, date) = for {...}

so when the left side matches the right side the variables will get the values

Example for a tuple:

val (a,b) = (1,2)
scala> val (a,b) = (1,2)
a: Int = 1
b: Int = 2
Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52