使用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
答案 0 :(得分:2)
它在Quoted[Boolean]
函数中没有显式返回类型equalTest
时起作用:
def equalTest(right: String) = quote {
infix"$left = $right".as[Boolean]
}