Skip to content

Commit 9fa3766

Browse files
authoredFeb 28, 2024
Merge pull request #2981 from som-snytt/tweak/named-args
Tweak language for named args and example
2 parents 604b354 + 9671946 commit 9fa3766

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed
 

Diff for: ‎_tour/named-arguments.md

+19-7
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,38 @@ When calling methods, you can label the arguments with their parameter names lik
2020
{% tab 'Scala 2 and 3' for=named-arguments-when-good %}
2121
```scala mdoc
2222
def printName(first: String, last: String): Unit =
23-
println(first + " " + last)
23+
println(s"$first $last")
2424

25-
printName("John", "Smith") // Prints "John Smith"
26-
printName(first = "John", last = "Smith") // Prints "John Smith"
27-
printName(last = "Smith", first = "John") // Prints "John Smith"
25+
printName("John", "Public") // Prints "John Public"
26+
printName(first = "John", last = "Public") // Prints "John Public"
27+
printName(last = "Public", first = "John") // Prints "John Public"
28+
printName("Elton", last = "John") // Prints "Elton John"
2829
```
2930
{% endtab %}
3031

3132
{% endtabs %}
3233

33-
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.
34+
This is useful when two parameters have the same type and the arguments could be accidentally swapped.
35+
36+
Notice that named arguments can be written in any order. However, once the arguments are not in parameter order, reading from left to right, then the rest of the arguments must be named.
37+
38+
In the following example, named arguments enable the middle parameter to be omitted. But in the error case, the first argument is out of order, so the second argument must be named.
3439

3540
{% tabs named-arguments-when-error %}
3641

3742
{% tab 'Scala 2 and 3' for=named-arguments-when-error %}
3843
```scala mdoc:fail
39-
printName(last = "Smith", "john") // error: positional after named argument
44+
def printFullName(first: String, middle: String = "Q.", last: String): Unit =
45+
println(s"$first $middle $last")
46+
47+
printFullName(first = "John", last = "Public") // Prints "John Q. Public"
48+
printFullName("John", last = "Public") // Prints "John Q. Public"
49+
printFullName("John", middle = "Quincy", "Public") // Prints "John Quincy Public"
50+
printFullName(last = "Public", first = "John") // Prints "John Q. Public"
51+
printFullName(last = "Public", "John") // error: positional after named argument
4052
```
4153
{% endtab %}
4254

4355
{% endtabs %}
4456

45-
Named arguments work with calls to Java methods, but only if the Java library in question was compiled with `-parameters`.
57+
Named arguments work with calls to Java methods, but only if the Java library in question was compiled with the `-parameters` flag.

0 commit comments

Comments
 (0)
Please sign in to comment.