From 22ce00b580cf511be8956b9825c69a0666e002d4 Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Tue, 16 Aug 2022 14:34:57 +0200 Subject: [PATCH] Translate tour/default-parameter-values to Scala 3 --- _tour/default-parameter-values.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index 60776fd228..44f4083951 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -13,29 +13,45 @@ redirect_from: "/tutorials/tour/default-parameter-values.html" Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters. +{% tabs default-parameter-values-1 %} +{% tab 'Scala 2 and 3' for=default-parameter-values-1 %} ```scala mdoc def log(message: String, level: String = "INFO") = println(s"$level: $message") log("System starting") // prints INFO: System starting log("User not found", "WARNING") // prints WARNING: User not found ``` +{% endtab %} +{% endtabs %} + 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. +{% tabs default-parameter-values-2 %} +{% tab 'Scala 2 and 3' for=default-parameter-values-2 %} ```scala mdoc class Point(val x: Double = 0, val y: Double = 0) val point1 = new Point(y = 1) ``` +{% endtab %} +{% endtabs %} + Here we have to say `y = 1`. Note that default parameters in Scala are not optional when called from Java code: +{% tabs default-parameter-values-3 %} +{% tab 'Scala 2 and 3' for=default-parameter-values-3 %} ```scala mdoc:reset // Point.scala class Point(val x: Double = 0, val y: Double = 0) ``` +{% endtab %} +{% endtabs %} +{% tabs default-parameter-values-4 %} +{% tab 'Java' for=default-parameter-values-4 %} ```java // Main.java public class Main { @@ -44,3 +60,5 @@ public class Main { } } ``` +{% endtab %} +{% endtabs %}