使用中缀

时间:2018-03-18 07:51:39

标签: scala dsl scala-macros quill.io

使用Quill的中缀操作时遇到问题 Scala 2.12.4 Quill 2.3.3

import io.getquill._
val ctx = new SqlMirrorContext(PostgresDialect, SnakeCase)
import ctx._

// infix for custom DB operation
implicit class StringQuotes(left: String) {
  def equalTest(right: String): Quoted[Boolean] = quote {
    infix"$left = $right".as[Boolean]
  }
}

case class User(name: String)

object User {
  // normal usage 
  def filter1() = quote {
    query[User]
      .filter(
        user => user.name == "Cool"
      )
  }
  //using infix for custom operation
  def filter2() = quote {
    query[User]
      .filter(
        user => user.name equalTest "Cool"
      )
  }
}
//pass, and generates a valid SQL.
ctx.run(User.filter1())

// fail
ctx.run(User.filter2())

错误信息不明确:

java.util.NoSuchElementException: value user
..... too long to show
Error while emitting main.scala

它只表示filter2是原因,并且没有说明哪一行出错。

Scastie在线编辑https://scastie.scala-lang.org/superChing/haYVkGLDSlKsOhYlaPdO1w

上的可运行代码段

1 个答案:

答案 0 :(得分:2)

它在Quoted[Boolean]函数中没有显式返回类型equalTest时起作用:

def equalTest(right: String) = quote {
    infix"$left = $right".as[Boolean]
}