layout | title | partof | num | next-page | previous-page | prerequisite-knowledge | redirect_from |
---|---|---|---|---|---|---|---|
tour | Default Parameter Values | scala-tour | 5 | named-arguments | classes | named-arguments, function syntax | /tutorials/tour/default-parameter-values.html |
Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters.
{% tabs default-parameter-values-1 %} {% tab 'Scala 2 and 3' for=default-parameter-values-1 %}
deflog(message: String, level: String="INFO") = println(s"$level: $message") log("System starting") // prints INFO: System starting log("User not found", "WARNING") // prints WARNING: User not found
{% endtab %} {% endtabs %}
The parameter level
has a default value so it is optional. On the last line, the argument "WARNING"
overrides the default argument "INFO"
. Where you might do overloaded methods in Java, you can use methods with optional parameters to achieve the same effect. However, if the caller omits an argument, any following arguments must be named.
{% tabs default-parameter-values-2 %} {% tab 'Scala 2 and 3' for=default-parameter-values-2 %}
classPoint(valx:Double=0, valy:Double=0) valpoint1=newPoint(y =1)
{% endtab %} {% endtabs %}
Here we have to say y = 1
.
Note that default parameters in Scala are not optional when called from Java code:
{% tabs default-parameter-values-3 %} {% tab 'Scala 2 and 3' for=default-parameter-values-3 %}
// Point.scalaclassPoint(valx:Double=0, valy:Double=0)
{% endtab %} {% endtabs %}
{% tabs default-parameter-values-4 %} {% tab 'Java' for=default-parameter-values-4 %}
// Main.javapublicclassMain { publicstaticvoidmain(String[] args) { Pointpoint = newPoint(1); // does not compile } }
{% endtab %} {% endtabs %}
Scala doesn't allow having two methods with default parameters and with the same name (overloaded). An important reason why is to avoid the ambiguity that can be caused due to the existence of default parameters. To illustrate the problem, let's consider the method declarations provided below:
{% tabs default-parameter-values-5 class=tabs-scala-version %} {% tab 'Scala 2' %}
objectA { deffunc(x: Int=34):Unitdeffunc(y: String="abc"):Unit }
{% endtab %} {% tab 'Scala 3' %}
objectA:deffunc(x: Int=34):Unitdeffunc(y: String="abc"):Unit
{% endtab %} {% endtabs %}
If we call A.func()
, compiler cannot know whether the programmer intended to call func(x: Int = 34)
or func(y: String = "abc")
.