scala - 我可以超载curried方法吗?

Car*_*zel 10 scala

有没有办法在Scala中重载多个参数列表的方法?我想这样做:

def foo(a: Int)(b: Int)(c: Int): Int

def foo(a: Int)(b: Int): Int
Run Code Online (Sandbox Code Playgroud)

我可以像这样定义它,但尝试调用第二个方法,如下所示:

foo(1)(1)
Run Code Online (Sandbox Code Playgroud)

使编译器抱怨"对重载定义的模糊引用",这似乎是合理的.有没有办法实现这样的事情?例如,在某些情况下,最后一个参数可能被认为是可选的.

Fre*_*Foo 8

你不能为此使用重载,因为由于currying会有两种foo方法只有它们的返回类型不同.

您可以使用Scala 2.8的可选参数和命名参数来近似,但您必须将该方法称为foo(1)(1)().例如,

object Hello {
  def foo(a : String = "Hello,") : String = a

  def main(args: Array[String]) {
    println(foo() + foo(" world!"))
  }
}
Run Code Online (Sandbox Code Playgroud)