Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method signature for a function with default values

Tags:

scala

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.
  }
}
like image 857
Mike Rylander Avatar asked Nov 07 '18 21:11

Mike Rylander


2 Answers

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
like image 78
jwvh Avatar answered Nov 09 '22 00:11

jwvh


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)
//         ^
like image 21
Jörg W Mittag Avatar answered Nov 09 '22 00:11

Jörg W Mittag