Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple parameters lists and default arguments

Tags:

scala

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?

like image 781
Sergey Alaev Avatar asked Dec 25 '22 05:12

Sergey Alaev


1 Answers

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)
like image 99
Nyavro Avatar answered Jan 11 '23 06:01

Nyavro