Compiling this code:
class Test {
def f(arg: Int)(defaultArg: String => Unit = println): Unit = ???
f(42)
}
fails with
missing argument list for method f in class Test
[error] Unapplied methods are only converted to functions when a function type is expected.
[error] You can make this conversion explicit by writing `f _` or `f(_)(_)` instead of `f`.
[error] f(42)
[error] ^
[error] one error found
Why does it fail? Does it introduce ambiguity? Is it possible to make it work w/o resorting to single parameters list?
The correct syntax for your case is the following:
f(42)()
You can make call to f(42) work by defining defaultArg as implicit:
def f(arg: Int)(implicit defaultArg: String => Unit = println): Unit = ???
Then you can call it:
f(42)
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