Named function literal parameter

Hi, is there any way of using named function literal parameter with default value in scala.

def functionA(f: (a:Int=0) => String):String = {
  f()
}

No.

Default values are a property of methods, not of functions; see this for more details.

Your method would always call the given f with the default value, so you can just write f(0) in the body. I don’t see any benefit to having it in the signature.

As an alternative, if the user of functionA should be able to pass that parameter optionally, you coud add that parameter to the method:

def functionA(f: Int => String, a: Int = 0):String = {
  f(a)
}

this would also allow functionA to be called with or without specifying a.

1 Like