2017-02-27 7 views
0

この機能のために後置記法を使用することはできません私は、私が使用して呼び出すしたいシンプルな機能を持っている後置記法なぜ私は

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_

は私をコンパイルしようとすると文句を言い

それは私が括弧内 runにパラメータを囲む場合は期待通りに動作します
...';' expected but string literal found. 
[error]  run SQL""".... 

、すなわち、次のエラーを取得
getColors(ids: Seq[UUID](implicit conn: Connection) = run(SQL"....") 

答えて

2

裸のメソッドの場合は後置記号はありませんが、名前付きオブジェクト(識別子付き)のメソッド呼び出ししかありません。単一のパラメータを持つオブジェクトのメソッド呼び出しには、中置表記法もあります。しかし、

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

あなたは中置記法を使用して、それを再度書き込むことができます。

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 = this copy "123" // this works 
} 

、あなたが書くことができます。

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