Skip to content

Commit 7f94d2b

Browse files
authored
Compare Scala 2 vs Scala 3 with tabs in Tour of Scala / Named Arguments
This is a simple page, so a good first try for document update :-) Waiting for feedback.
1 parent d4bede1 commit 7f94d2b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: _tour/named-arguments.md

+24
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ redirect_from:
1515

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

18+
{% tabs named-arguments-when-good class=tabs-scala-version %}
19+
20+
{% tab 'Scala 2' for=named-arguments-when-good %}
1821
```scala mdoc
1922
def printName(first: String, last: String): Unit = {
2023
println(first + " " + last)
@@ -24,10 +27,31 @@ printName("John", "Smith") // Prints "John Smith"
2427
printName(first = "John", last = "Smith") // Prints "John Smith"
2528
printName(last = "Smith", first = "John") // Prints "John Smith"
2629
```
30+
{% endtab %}
31+
32+
{% tab 'Scala 3' for=named-arguments-when-good %}
33+
```scala mdoc
34+
def printName(first: String, last: String): Unit =
35+
println(first + " " + last)
36+
37+
printName("John", "Smith") // Prints "John Smith"
38+
printName(first = "John", last = "Smith") // Prints "John Smith"
39+
printName(last = "Smith", first = "John") // Prints "John Smith"
40+
```
41+
{% endtab %}
42+
43+
{% endtabs %}
44+
2745
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.
2846

47+
{% tabs scala-2-and-3-demo %}
48+
49+
{% tab 'Scala 2 and 3' for=named-arguments-when-error %}
2950
```scala mdoc:fail
3051
printName(last = "Smith", "john") // error: positional after named argument
3152
```
53+
{% endtab %}
54+
55+
{% endtabs %}
3256

3357
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)