21

I was walking through the documentation of Slick to setup a quick working prototype.

In the Mapped Tables section I see a <> operator in the example mentioned but can't find any documentation for that anywhere. Need help in understanding this.

Simson
  • 3,373
  • 2
  • 24
  • 38
Som Bhattacharyya
  • 3,972
  • 35
  • 54

3 Answers3

29

The <> operator defines a relation between a Row in the Table and a case class.

case class User(id: Option[Int], first: String, last: String)

ROW            |id             | first        | last        |

So the data first is taken out of the Tabels as an n-tuple (left side of <>) and then transformed to the case class (right side of <>).

To make the transformation of the case class work one needs two kinds of methods:

Row to n-tuple to case class.

scala> User.tupled
res6: ((Option[Int], String, String)) => User = <function1>

So this function can create a User when given a triple (Option[Int], String, String) as an argument.

case class to n-tuple to be written in DB.

scala> User.unapply _
res7: User => Option[(Option[Int], String, String)] = <function1>

This function provides the functionality with the other way round. Given a user it can extract a triple. This pattern is called an Extractor. Here you can learn more about this: http://www.scala-lang.org/old/node/112

Community
  • 1
  • 1
Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52
4

It's not a scala operator, it's a method defined by slick's ShapedValue class

As you can see in the documentation you linked, it's used to map a projection to and from a case class providing two methods

 def * = (id.?, first, last) <> (User.tupled, User.unapply)
Giovanni Caporaletti
  • 5,426
  • 2
  • 26
  • 39
2

If you clone the Slick source repo and grep for def <>, you'll find that <> is a method of ShapedValue that returns a MappedProjection.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • Yes checking out the code helps a lot. Am still new to Scala so facing frequent doubts on obvious things. :D Thanks ! – Som Bhattacharyya Mar 07 '16 at 10:16
  • So i see the <> method is invoked on a tuple. How does scala resolve the method on the ShapedValue object ? I think its being done by a implicit conversion but cant figure it out. Let me know if i need to raise another question. – Som Bhattacharyya Mar 08 '16 at 09:26
  • 1
    Grepping for the regex `implicit.*def.*: ShapedValue`, it looks like the implicit that provides `ShapedValue` comes from [`BasicProfile`](https://github.com/slick/slick/blob/3b3bd36c93c6d9c63b0471ff4d8409f913954b2b/slick/src/main/scala/slick/basic/BasicProfile.scala#L53). Though I'm not familiar with Slick at all, and since that documentation you linked to doesn't include any import statements, I'm not sure how that implicit gets into scope. You may want to open another question. – Chris Martin Mar 08 '16 at 09:45