为什么我不能对这个函数使用后缀表示法

Why can't I use postfix notation for this function

我想使用后缀表示法调用一个简单的函数

import anorm._
class SimpleRepository {
  private def run(sql: SimpleSql[Row]) = sql.as(SqlParser.scalar[String].*)

  // this is how i'd like to call the method
  def getColors(ids: Seq[UUUID])(implicit conn: Connection) = run SQL"""select color from colors where id in $ids""" 

  def getFlavors(ids: Seq[UUID])(implicit conn: Connection) = run SQL"""select flavor from flavors where id in $ids""" 
}

IntelliJ 抱怨 Expression of type SimpleSql[Row] does not conform to expected type A_

当我尝试编译时出现以下错误

...';' expected but string literal found.
[error]       run SQL"""....

如果我将 run 的参数括在括号中,即

,它会按预期工作
getColors(ids: Seq[UUID](implicit conn: Connection) = run(SQL"....")

裸方法没有后缀表示法,只有对命名对象(带有标识符)的方法调用。对于具有单个参数的对象的方法调用,也有中缀表示法。

以下是将后缀和中缀表示法与方法一起使用的方法:

case class Foo(value: String) {
    def run() = println("Running")
    def copy(newValue: String) = Foo(newValue)
}

scala> val foo = Foo("abc")
foo: Foo = Foo(abc)

scala> foo run() // Postfix ops in an object `foo`, but it is 
Running          // recommended you enable `scala.language.postfixOps`

scala> foo copy "123" // Using copy as an infix operator on `foo` with "123"
res3: Foo = Foo(123)

然而,这不起作用:

case class Foo(value: String) {
    def copy(newValue: String) = Foo(newValue)
    def postfix = copy "123" // does not work
}

不过您可以使用中缀表示法重写它:

case class Foo(value: String) {
    def copy(newValue: String) = Foo(newValue)
    def postfix = this copy "123" // this works
}

在你的情况下,你可以这样写:

this run SQL"""select flavor from flavors where id in $ids"""