layout | title | partof | num | next-page | previous-page | prerequisite-knowledge | redirect_from | ||
---|---|---|---|---|---|---|---|---|---|
tour |
Named Arguments |
scala-tour |
6 |
traits |
default-parameter-values |
function-syntax |
|
When calling methods, you can label the arguments with their parameter names like so:
{% tabs named-arguments-when-good class=tabs-scala-version %}
{% tab 'Scala 2' for=named-arguments-when-good %}
def printName(first: String, last: String): Unit = {
println(first + " " + last)
}
printName("John", "Smith") // Prints "John Smith"
printName(first = "John", last = "Smith") // Prints "John Smith"
printName(last = "Smith", first = "John") // Prints "John Smith"
{% endtab %}
{% tab 'Scala 3' for=named-arguments-when-good %}
def printName(first: String, last: String): Unit =
println(first + " " + last)
printName("John", "Smith") // Prints "John Smith"
printName(first = "John", last = "Smith") // Prints "John Smith"
printName(last = "Smith", first = "John") // Prints "John Smith"
{% endtab %}
{% endtabs %}
Notice how the order of named arguments can be rearranged. However, if some arguments are named and others are not, the unnamed arguments must come first and in the order of their parameters in the method signature.
{% tabs scala-2-and-3-demo %}
{% tab 'Scala 2 and 3' for=named-arguments-when-error %}
printName(last = "Smith", "john") // error: positional after named argument
{% endtab %}
{% endtabs %}
Named arguments work with calls to Java methods, but only if the Java library in question was compiled with -parameters
.