我可以定义一个函数为:

def print(n:Int, s:String = "blah") {}
print: (n: Int,s: String)Unit

我可以这样称呼:
print(5)
print(5, "testing")

如果我 curry 以上:
def print2(n:Int)(s:String = "blah") {}
print2: (n: Int)(s: String)Unit

我无法使用1个参数来调用它:
print2(5)
<console>:7: error: missing arguments for method print2 in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
       print2(5)

我必须提供两个参数。有什么办法解决吗?

最佳答案

您不能使用默认参数忽略():

scala> def print2(n:Int)(s:String = "blah") {}
print2: (n: Int)(s: String)Unit

scala> print2(5)()

虽然它适用于隐式:
scala> case class SecondParam(s: String)
defined class SecondParam

scala> def print2(n:Int)(implicit s: SecondParam = SecondParam("blah")) {}
print2: (n: Int)(implicit s: SecondParam)Unit

scala> print2(5)

关于scala - 默认参数为currying,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5074421/

10-09 01:53