Is there a way to overload methods in Scala that take multiple parameter lists? E.g. I'd like to do this:
def foo(a: Int)(b: Int)(c: Int): Int
def foo(a: Int)(b: Int): Int
I can define it like this, but trying to call the second method like this:
foo(1)(1)
makes the compiler complain about "ambiguous reference to overloaded definition", which seems justified. Is there a way to achieve something like this? The last parameter might be considered optional in some cases, for example.
You can't use overloading for this, since due to the currying there would be two foo
methods differing only in their return type.
You can use Scala 2.8's optional and named parameters to approximate this, but you'd have to call the method as foo(1)(1)()
. E.g.,
object Hello {
def foo(a : String = "Hello,") : String = a
def main(args: Array[String]) {
println(foo() + foo(" world!"))
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With