Skip to content

Commit 29a4ff7

Browse files
adpi2julienrf
authored andcommitted
Translate tour/default-parameter-values to Scala 3
1 parent ff54f5b commit 29a4ff7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

_tour/default-parameter-values.md

+18
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,45 @@ redirect_from: "/tutorials/tour/default-parameter-values.html"
1313

1414
Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters.
1515

16+
{% tabs default-parameter-values-1 %}
17+
{% tab 'Scala 2 and 3' for=default-parameter-values-1 %}
1618
```scala mdoc
1719
def log(message: String, level: String = "INFO") = println(s"$level: $message")
1820

1921
log("System starting") // prints INFO: System starting
2022
log("User not found", "WARNING") // prints WARNING: User not found
2123
```
24+
{% endtab %}
25+
{% endtabs %}
26+
2227

2328
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.
2429

30+
{% tabs default-parameter-values-2 %}
31+
{% tab 'Scala 2 and 3' for=default-parameter-values-2 %}
2532
```scala mdoc
2633
class Point(val x: Double = 0, val y: Double = 0)
2734

2835
val point1 = new Point(y = 1)
2936
```
37+
{% endtab %}
38+
{% endtabs %}
39+
3040
Here we have to say `y = 1`.
3141

3242
Note that default parameters in Scala are not optional when called from Java code:
3343

44+
{% tabs default-parameter-values-3 %}
45+
{% tab 'Scala 2 and 3' for=default-parameter-values-3 %}
3446
```scala mdoc:reset
3547
// Point.scala
3648
class Point(val x: Double = 0, val y: Double = 0)
3749
```
50+
{% endtab %}
51+
{% endtabs %}
3852

53+
{% tabs default-parameter-values-4 %}
54+
{% tab 'Java' for=default-parameter-values-4 %}
3955
```java
4056
// Main.java
4157
public class Main {
@@ -44,3 +60,5 @@ public class Main {
4460
}
4561
}
4662
```
63+
{% endtab %}
64+
{% endtabs %}

0 commit comments

Comments
 (0)