问题
Can anyone tell me why in this case:
Query(Users) foreach {case (userId, userName) =>
println(userId + ", " + userName) }
Scala recognizes userId, but in this case:
val l = List[(Int, String)]()
Query(Users) foreach {
case (userId, userName) =>
l::(foo(List[(userId, userName)]))
}
it doesnt? (as in, the userId on the right of the "=>" is recognized in the second case but not the first)
Users is a slick-mounted database that looks like this:
object Users extends Table[(Int, String)]("Users") {
def userId = column[Int]("UserId", O.PrimaryKey, O.AutoInc)
def userName = column[String]("UserName")
def * = userId ~ userName
}
回答1:
I think what you mean is:
l::(foo(List((userId, userName))))
When you put stuff between the square brackets, you are attempting to type the list and I assume you actually wanted to add the Tuple
of userId
and userName
to a List instead.
You could also write it like this if all you wanted to do was put it into that List
and you did not need that Tuple
extractor:
Query(Users) foreach { tup =>
l::(foo(List(tup)))
}
来源:https://stackoverflow.com/questions/17624657/slick-table-query-trouble-with-recognizing-values