String
高階関数を適用できる新しい apply メソッドで拡張しようとしています。例:
case class A(s:String, f: List[String] => List[String])
val f: List[String] => List[String] = { ... stuff ... }
"foo"{f} // == A("foo", f)
そのため、関数を受け取る apply メソッドを使用して、文字列から何かへの暗黙的な変換を定義しましたList[String] => List[String]
。
implicit def c(s:String) = new {
def apply(f: List[String] => List[String]) = A(s, f)
}
しかし、私がそれを使用しようとすると、変換は Predef に変換String
されるものと衝突しStringOps
ます。
scala> "foo"{f}
<console>:19: error: type mismatch;
found : java.lang.String
required: ?{val apply: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method c in object $iw of type (s: String)java.lang.Object{def apply(f: (List[String]) => List[String]): A}
and method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps
are possible conversion functions from java.lang.String to ?{val apply: ?}
"foo"{f}
^
required: ?{val apply: ?}
私のタイプの引数を取るメソッド ( ) ではなく、一般的な適用メソッド ( ) を探すのはなぜList[String] => List[String]
ですか?
編集:
変数を表現するために裸の文字列を使用しないようにすることでこれを解決しました(プロジェクトではgithubで作業しています)。したがって、次のようになります。
case class param(val v: String) {
def apply(f: Emit.Selector) = Variable(v, f)
}
val $foo = param("foo")
foo{selector} // works fine
そして、暗黙を使用する必要はありません。
さらにアップデート
scala は、検索時に暗黙の結果の型で型パラメーターを検索するようです。私はこれを機能させますが、関数パラメーターと適用メソッドを使用したシナリオは機能しません。どうして?
scala> class A()
defined class A
scala> class B()
defined class B
scala> implicit def one(s:String) = new {
| def a(a:A) = s + " A"
| }
one: (s: String)java.lang.Object{def a(a: A): java.lang.String}
scala> implicit def another(s:String) = new {
| def a(b:B) = s + " B"
| }
another: (s: String)java.lang.Object{def a(b: B): java.lang.String}
scala> "hello" a new A
res1: java.lang.String = hello A
scala> "hello" a new B
res2: java.lang.String = hello B