8

我可以将函数定义为:

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

我可以这样称呼它:

print(5) 
print(5, "testing")

如果我咖喱上面:

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)

我必须提供这两个参数。有没有办法解决这个问题?

4

1 回答 1

17

您不能省略()默认参数:

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)
于 2011-02-22T06:05:54.333 回答