Scala:将当前函数作为参数传递

Scala:将当前函数作为参数传递,scala,functional-programming,Scala,Functional Programming,是否可以执行以下操作 def takeCurriedFnAsArg(f: (Int)(implicit MyClass) => Result) 是的,这是可能的 当第二个curried参数标记为implicit时,函数似乎不是类型 Int => (MyClass => Result) => ResultOfFunction 如果当前的高阶函数参数是一个常规参数,它将是;相反,它看起来是这样的: Int => ResultOfFunction 下面是一个简单的

是否可以执行以下操作

def takeCurriedFnAsArg(f: (Int)(implicit MyClass) => Result)
是的,这是可能的

当第二个curried参数标记为
implicit
时,函数似乎不是类型

Int => (MyClass => Result) => ResultOfFunction 
如果当前的高阶函数参数是一个常规参数,它将是;相反,它看起来是这样的:

Int => ResultOfFunction
下面是一个简单的例子:

scala> def curriedFn(i : Int)(implicit func : String => Int) : Boolean = (i + func("test!")) % 2 == 0
curriedFn: (i: Int)(implicit func: String => Int)Boolean

scala> implicit val fn : String => Int = s => s.length
fn: String => Int = <function1>

scala> curriedFn _
res4: Int => Boolean = <function1>
现在,如果第二个函数参数不是隐式的:

scala> def curriedNonImplicit(i : Int)(fn : String => Int) : Boolean = (i + fn("test!")) % 2 == 0
curriedNonImplicit: (i: Int)(fn: String => Int)Boolean

scala> curriedNonImplicit _
res5: Int => ((String => Int) => Boolean) = <function1>

您必须直接在方法内部指定函数,因为以前没有隐式提供。

感谢您用详尽的示例解释了这两种情况(有隐式和无隐式)。
scala> def curriedNonImplicit(i : Int)(fn : String => Int) : Boolean = (i + fn("test!")) % 2 == 0
curriedNonImplicit: (i: Int)(fn: String => Int)Boolean

scala> curriedNonImplicit _
res5: Int => ((String => Int) => Boolean) = <function1>
scala> def baz(func : Int => (String => Int) => Boolean) = if(func(3)(s => s.length)) "True!" else "False!"
baz: (func: Int => ((String => Int) => Boolean))String

scala> baz(curriedNonImplicit)
res6: String = True!