Skip to content

Commit e1c54ad

Browse files
authored
Merge pull request #2521 from bishabosha/add-tabs-tour-named-args
Add tabs tour named args
2 parents 8d31b5a + 52bc751 commit e1c54ad

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

_tour/named-arguments.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,38 @@ next-page: traits
88
previous-page: default-parameter-values
99
prerequisite-knowledge: function-syntax
1010

11-
redirect_from:
11+
redirect_from:
1212
- "/tutorials/tour/named-arguments.html"
1313
- "/tutorials/tour/named-parameters.html"
1414
---
1515

1616
When calling methods, you can label the arguments with their parameter names like so:
1717

18+
{% tabs named-arguments-when-good %}
19+
20+
{% tab 'Scala 2 and 3' for=named-arguments-when-good %}
1821
```scala mdoc
19-
def printName(first: String, last: String): Unit = {
22+
def printName(first: String, last: String): Unit =
2023
println(first + " " + last)
21-
}
2224

2325
printName("John", "Smith") // Prints "John Smith"
2426
printName(first = "John", last = "Smith") // Prints "John Smith"
2527
printName(last = "Smith", first = "John") // Prints "John Smith"
2628
```
29+
{% endtab %}
30+
31+
{% endtabs %}
32+
2733
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.
2834

35+
{% tabs named-arguments-when-error %}
36+
37+
{% tab 'Scala 2 and 3' for=named-arguments-when-error %}
2938
```scala mdoc:fail
3039
printName(last = "Smith", "john") // error: positional after named argument
3140
```
41+
{% endtab %}
42+
43+
{% endtabs %}
3244

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

0 commit comments

Comments
 (0)