Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

scala - What does the <> operator do in Slick?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...