Scala `s <> operator value

I am trying to learn Scala to use it in the Play Framework. Now I am dealing with Play for Scala + Slick for the database level, and I am using a code snippet from a tutorial that I don’t understand, and I can’t find the information in the Scala manual.

That's what. I have a model called Entry. It is defined as a case class, and I have a companion class extended from a table.

 case class Entry(id: Int, name: String) class EntryTable(tag: Tag) extends Table[Entry](tag, "entries") { def id = column[Int]("id", O.PrimaryKey) def name = column[String]("name") def * = (id, name) <> (Entry.tupled, Entry.unapply(_)) } 

What I don't understand is the last line with def * . I know this has something to do with thinking. Basically, I would understand the def * = (id, name) , but what the other part means. I can not find the meaning of the operator <> ? Can someone explain this to me?

+5
source share
1 answer

The <> operator means the projection between the tuple (Int, String) and the case Entry class.

This can be explained in steps:

  • To return objects, Slick needs to design * ( * from SELECT * in SQL)
  • This projection can be defined in various ways, but the <> operator is most often used. Its simplified signature is as follows: <>[T, C](apply: T => C, unapply: C => Option[T])
  • (id, name) builds a tuple (Int, String) (simplified, actually it (Rep[Int], Rep[String]) , but Slick does not use it later)
  • <> maps it to Entry because Entry has apply with signature (Int, String) => Entry , which converts .tupled to ((Int, String)) => Entry and unapply with signature Entry => Option[(Int, String)] .
  • Now you have a projection * that can create Entry objects from rows and database rows from objects.
+9
source

Source: https://habr.com/ru/post/1247754/


All Articles