4

以下の作品:

object X extends (Int => String => Long) {

  def apply(x:Int):String => Long = ???
}

パラメータを使用してapply関数を入力するにはどうすればよいですか?implicit

私は次の方法を持っています:

def apply(x:Int)(implicit y:String):Long = ???

関数の種類はどのように記述すればよいですか?

object X extends <functionType> {
  def apply(x:Int)(implicit y:String):Long = ???
}

アップデート

次のように定義できます。

object X extends (Int => String => Long) { 

  def apply(x:Int):(String => Long) = ???
  def apply(x:Int)(implicit y:String):Long = ???; 
}

しかし、それを呼び出しても機能しません:

error: ambiguous reference to overloaded definition,
both method apply in object X of type (x: Int)(implicit y: String)Long
and  method apply in object X of type (x: Int)String => Long
match argument types (Int)
              X(3)
              ^
4

1 に答える 1

3

私の頭に浮かぶ唯一のことはこれです:

object X {
  def apply(i: Int)(implicit y: String): Long = ???

  implicit def xToFun(x: X.type)(implicit y: String): Int => Long = x(_)
}

X暗黙的な変換により、必要な場所で使用できるようInt => Longになり、暗黙的なStringパラメーターは、その変換が適用されたときに解決されます (X.apply実際に呼び出されたときではありません)。

val fun: Int => Long = X //error, no implicit String in scope

implicit val s = "fuu"
val fun: Int => Long = X //OK
于 2013-10-04T19:29:53.883 に答える