Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala`s <> operator meaning

I am trying to learn Scala in order to use it with Play Framework. Now I am dealing with Play for Scala + Slick for database layer and I am using a piece of code from tutorial which I do not understand and I am unable to find any info in Scala manual.

Here is the thing. I have got model named Entry. It is defined as case class and I have a companion class extended from 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 do not understand, is the last line with def *. I know, that it has something to do with reflection. Basically I would understand the part def * = (id, name), but what does mean the other part. I cannot find meaning of operator <>? Can anyone explain this to me?

like image 821
spidla Avatar asked Apr 22 '16 20:04

spidla


People also ask

How to use not equal in Scala?

Not Equal To(!=) operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the '==' operator.

What is the difference between :: and #:: In Scala What is the difference between ::: and in Scala?

In general: :: - adds an element at the beginning of a list and returns a list with the added element. ::: - concatenates two lists and returns the concatenated list.

What does =!= Mean in Scala?

It has no special meaning whatsoever. It is also not a well-known method name in Scala. It seems to come from some library; you need to look at the documentation of whatever library you are using to figure out what it does.

What does :+ mean in Scala?

On Scala Collections there is usually :+ and +: . Both add an element to the collection. :+ appends +: prepends. A good reminder is, : is where the Collection goes. There is as well colA ++: colB to concat collections, where the : side collection determines the resulting type.


1 Answers

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

It can be explained in steps:

  1. In order to return objects, Slick needs a * projection (* is from SELECT * in SQL)
  2. This projection can be defined in various ways, but the most common is using <> operator. It's signature, when simplified, looks like: <>[T, C](apply: T => C, unapply: C => Option[T])
  3. (id, name) constructs a tuple of (Int, String) (simplified, actually it's (Rep[Int], Rep[String]), but Slick will unlift it later)
  4. <> maps it to Entry, because Entry has apply with signature (Int, String) => Entry, which is transformed by .tupled to ((Int, String)) => Entry, and unapply with signature Entry => Option[(Int, String)].
  5. Now you have * projection that can construct Entry objects from database rows and rows from objects.
like image 105
Maxim Avatar answered Oct 03 '22 03:10

Maxim