<> 运算符在 Slick 中有什么作用?
What does the <> operator do in Slick?
我正在浏览 documentation of Slick 以设置一个快速工作原型。
在“映射表”部分中,我在提到的示例中看到了一个 <>
运算符,但在任何地方都找不到该运算符的任何文档。需要帮助来理解这一点。
不是scala操作符,是slick的ShapedValueclass
定义的方法
正如您在链接的文档中看到的那样,它用于将投影映射到案例 class 并提供两种方法
def * = (id.?, first, last) <> (User.tupled, User.unapply)
如果你clone the Slick source repo and grep for def <>
, you'll find that <>
is a method of ShapedValue
that returns a MappedProjection
.
<>
运算符定义 Table
中的 Row
和 case class
.
之间的关系
case class User(id: Option[Int], first: String, last: String)
ROW |id | first | last |
所以数据首先从表格中取出作为n-tuple
(<>
的左侧),然后转换为case class
([=13=的右侧) ]).
要使案例class的转换生效,需要两种方法:
Row
到 n-tuple
到 case class
.
scala> User.tupled
res6: ((Option[Int], String, String)) => User = <function1>
因此,当给定一个三元组 (Option[Int], String, String)
作为参数时,此函数可以创建一个 User
。
case class
到 n-tuple
写入数据库。
scala> User.unapply _
res7: User => Option[(Option[Int], String, String)] = <function1>
这个函数提供了相反的功能。给定一个用户,它可以提取一个三元组。这种模式称为 Extractor
。您可以在这里了解更多相关信息:http://www.scala-lang.org/old/node/112
我正在浏览 documentation of Slick 以设置一个快速工作原型。
在“映射表”部分中,我在提到的示例中看到了一个 <>
运算符,但在任何地方都找不到该运算符的任何文档。需要帮助来理解这一点。
不是scala操作符,是slick的ShapedValueclass
定义的方法正如您在链接的文档中看到的那样,它用于将投影映射到案例 class 并提供两种方法
def * = (id.?, first, last) <> (User.tupled, User.unapply)
如果你clone the Slick source repo and grep for def <>
, you'll find that <>
is a method of ShapedValue
that returns a MappedProjection
.
<>
运算符定义 Table
中的 Row
和 case class
.
case class User(id: Option[Int], first: String, last: String)
ROW |id | first | last |
所以数据首先从表格中取出作为n-tuple
(<>
的左侧),然后转换为case class
([=13=的右侧) ]).
要使案例class的转换生效,需要两种方法:
Row
到 n-tuple
到 case class
.
scala> User.tupled
res6: ((Option[Int], String, String)) => User = <function1>
因此,当给定一个三元组 (Option[Int], String, String)
作为参数时,此函数可以创建一个 User
。
case class
到 n-tuple
写入数据库。
scala> User.unapply _
res7: User => Option[(Option[Int], String, String)] = <function1>
这个函数提供了相反的功能。给定一个用户,它可以提取一个三元组。这种模式称为 Extractor
。您可以在这里了解更多相关信息:http://www.scala-lang.org/old/node/112