Slick table Query: Trouble with recognizing values

落花浮王杯 提交于 2019-12-11 19:33:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!