2013-10-04 43 views
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) 
      ^

回答

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 
+0

你當然給了我一些想法,謝謝! – EECOLOR