Почему в Slick 3.0 метод `result` может быть применен к объекту `Query`?

Я использую Slick 3.0: https://github.com/slick/slick/tree/ 3.0.0

Примеры кодов выглядят так:

class Coffees(tag: Tag) extends Table[(String, Double)](tag, "COFFEES") {
  def name = column[String]("COF_NAME", O.PrimaryKey)
  def price = column[Double]("PRICE")
  def * = (name, price)
}
val coffees = TableQuery[Coffees]
val coffeeNames: Future[Seq[Double]] = db.run(
  coffees.map(_.price).result
)

Я думаю, что метод result в coffees.map(_.price).result описан здесь:

http://www.scala-lang.org/api/2.11.5/index.html#scala.collection.mutable.Builder@result():To

который является методом класса Builder.

Однако coffees.map(_.price) является классом Query, а не классом Builder, а класс Query не является подклассом класса Builder. Более того, кажется, что неявного преобразования из класса Query в класс Builder не существует. А в классе Query нет метода result.

Так как же result применяется к объекту Query. У кого-нибудь есть идеи по этому поводу?


person Hanfei Sun    schedule 12.05.2015    source источник


Ответы (2)


Метод result не из класса Builder. Он определен в классе QueryActionExtensionMethodsImpl в Файл JdbcActionComponent.scala.

person Yadu Krishnan    schedule 12.05.2015

В /slick/profile/BasicProfile.scala,

  trait API extends CommonAPI with CommonImplicits {
    implicit def repQueryActionExtensionMethods[U](rep: Rep[U]): QueryActionExtensionMethods[U, NoStream] =
      createQueryActionExtensionMethods[U, NoStream](queryCompiler.run(rep.toNode).tree, ())
    implicit def streamableQueryActionExtensionMethods[U, C[_]](q: Query[_,U, C]): StreamingQueryActionExtensionMethods[C[U], U] =
      createStreamingQueryActionExtensionMethods[C[U], U](queryCompiler.run(q.toNode).tree, ())
    implicit def runnableCompiledQueryActionExtensionMethods[RU](c: RunnableCompiled[_, RU]): QueryActionExtensionMethods[RU, NoStream] =
      createQueryActionExtensionMethods[RU, NoStream](c.compiledQuery, c.param)
    implicit def streamableCompiledQueryActionExtensionMethods[RU, EU](c: StreamableCompiled[_, RU, EU]): StreamingQueryActionExtensionMethods[RU, EU] =
      createStreamingQueryActionExtensionMethods[RU, EU](c.compiledQuery, c.param)
    // Applying a CompiledFunction always results in only a RunnableCompiled, not a StreamableCompiled, so we need this:
    implicit def runnableStreamableCompiledQueryActionExtensionMethods[R, RU, EU, C[_]](c: RunnableCompiled[Query[R, EU, C], RU]): StreamingQueryActionExtensionMethods[RU, EU] =
      createStreamingQueryActionExtensionMethods[RU, EU](c.compiledQuery, c.param)
    // This only works on Scala 2.11 due to SI-3346:
    implicit def recordQueryActionExtensionMethods[M, R](q: M)(implicit shape: Shape[_ <: FlatShapeLevel, M, R, _]): QueryActionExtensionMethods[R, NoStream] =
      createQueryActionExtensionMethods[R, NoStream](queryCompiler.run(shape.toNode(q)).tree, ())
  }

Существует метод repQueryActionExtensionMethods, который преобразует Rep (надкласс Query) в QueryActionExtensionMethods.

person Hanfei Sun    schedule 12.05.2015