In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of indirectHelloName
that the provided function must provide a default value for the second parameter?
def helloName(name: String, greating: String = "hello"): Unit = {
println(s"$greating $name")
}
def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}
One thing you could do is move the default value from the parameter list to inside the method.
def helloName(in :String*) :Unit =
println(in.lift(1).getOrElse("hello") + ", " + in.head)
def indirectHelloName(name: String, function: (String*) => Unit): Unit =
if (name == "Ted") function(name, "Custom Greeting")
else function(name) //use default
usage:
indirectHelloName("Ted", helloName) //Custom Greeting, Ted
indirectHelloName("Tam", helloName) //hello, Tam
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of
indirectHelloName
that the provided function must provide a default value for the second parameter?
A function cannot have an optional parameter with default argument, therefore there is no way to specify one:
val f = (a: Int, b: Int) => a + b
//⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05
val g = (a: Int, b: Int = 5) => a + b
// <console>:1: error: ')' expected but '=' found.
// val g = (a: Int, b: Int = 5) => a + b
// ^
val h = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int) = a + b
}
//⇒ h: (Int, Int) => Int = <function2>
val i = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int = 5) = a + b
}
//⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>
i(3, 5)
//⇒ res: Int = 8
i(3)
// <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
// Unspecified value parameter v2.
// h(3)
// ^
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