我正在研究Scala的curry函数,但遇到了以下问题。
scala> def add(x: Int, y: Int): Int = x + y
add: (x: Int, y: Int)Int
scala> val addCurried = Function.curried(add _)
<console>:11: error: value curried is not a member of object Function
val addCurried = Function.curried(add _)
scala> val addCurried = (add _).curried
addCurried: Int => (Int => Int) = <function1>
然后,我查看了Scala的API(Function),发现对象Function
没有方法curried
。但是,根据这篇文章(Function Currying in Scala),它应该有效。
另一方面,我也尝试将一个咖喱功能转移到一个没有咖喱的功能中:
scala> def adder(x:Int)(y:Int): Int = x + y
adder: (x: Int)(y: Int)Int
scala> val addUncurried = Function.uncurried(adder _)
addUncurried: (Int, Int) => Int = <function2>
scala> val addUncurried = (adder _).uncurried
<console>:11: error: value uncurried is not a member of Int => (Int => Int)
val addUncurried = (adder _).uncurried
在这种情况下,由于uncurried
是Function
的成员,Function.uncurried(adder _)
正在运作。但是这个(adder _).uncurried
并没有起作用。
任何人都可以解释为什么在Scala中这样设计?并且