scala - The elements type of for comprehension in ScalaQuery -


I have found something interesting when I follow the Quilacuri Queries tutorial which I do not know very well.

And the query is used here:

  object main {val database = database .forURL ("jdbc: sqlite: sample.db", driver = "org.sqlite .JDBC ") DEF Main (Args: Array [String]) {Sessions with database {val query1 = for (user & lt; - query (user)) produce user.id ~ user.last val for query2 = (user & Lt; - User if user.id> 5) yield user.id ~ user-end Mm}}}   

In this case, you can see that some query1 and query2 Use .id , it seems that the user is the type of singleton object user which I have defined above so that all the methods defined on that object are .

But if I try to run a direct query without the keyword , for example:

  (user & lt; - user If user.id> 5) {println ("UserID:" + user.id)}   

In this case, the compiler complains:

 < Code> [error] src / main / scala / test.scala: 23: value is not a member of id (int, string, string) [error] println ("UserID:" + user.id)   

It seems that the println statement in the user is the type of Tuple3 and if I If I use simple code like the user , then it will work. for user ; User = user.id & gt; 5) {println ("UserID:" + user._1)}

And you can see, I'm still in the guard for expression user.id What is the type of user ? Why can I use the user.id in the guards and yield blocks, but do I need to use it in the body of expression as a tuple?

Thank you.

In the first code snippet:

  for val query1 = ( User & lt; - The query (user)) is the alias for the user.id user   

user is object user because the query's map The method is equivalent to the DIF map [F] (F: E = & gt; F): query [F] , the first code snippet is:

  query (user) .map (user = & gt; user.id ~ user.last)   

then e is sub Ogkrta 's type and user example parameter f (E): F .

If you want to travel the line as an object, then you need to define users as the following:

  import Do org.scalaquery.ql.basic {BasicTable = & gt; Table} object user table [user] (user ") {def id = column [int] (" id ", not note) def first = column [string] (" first ", o not) def previous = column [string ] ("Last", o nanal) def * = id ~ first ~ last < & Gt; (User, user.updump_)} case class user (id: IT, first: string, last: string)   

and then

  (User & lt; - User if user.id> 5) {println ("UserID:" + user.id) // User user example}    

Comments