From 2655b617c12003aff76bd48619eaac0ce3f053db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Sun, 20 Nov 2022 21:31:27 +0300 Subject: [PATCH 1/9] Add subsection "Default Parameters for Overloaded Methods" Add a subsection to [Default Parameter Values](https://docs.scala-lang.org/tour/default-parameter-values.html) as to why we can't use default parameters for more than one overloaded method. Sources: * https://groups.google.com/g/scala-user/c/FyQK3-cqfaY/m/fXLHr8QsW_0J * https://stackoverflow.com/a/10927256/16530078 --- _tour/default-parameter-values.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index 44f4083951..facaae9283 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -62,3 +62,24 @@ public class Main { ``` {% endtab %} {% endtabs %} + +### Default Parameters for Overloaded Methods + +It's important to notice that Scala doesn't allow having two methods with default parameters and with the same name (overloaded). The reason is to keep a deterministic naming-scheme for the generated methods which return default arguments. + +For example, if you write `def f(a: Int = 1)`, the compiler generates `def f$default$1 = 1`. If you have two overloads with defaults on the same parameter position, then we would need a different naming scheme. In this situation, it is preferred to keep the generated byte-code stable over multiple compiler runs. + +The other reason is to avoid the ambiguity that can be caused due to the existence of default parameters. To illustrate the problem, let's consider the method declarations provided below: + +{% tabs default-parameter-values-5 %} +{% tab 'Scala 2 and 3' for=default-parameter-values-5 %} +```scala mdoc:reset +object A { + def func(x: Int = 34) {} + def func(y: String = "abc") {} +} +``` +{% endtab %} +{% endtabs %} + +If we call `A.func()`, compiler cannot know whether the programmer intended to call `func(x: Int = 34)` or `func(y: String = "abc")`. From 6f6a87f99d42d5fe3185d061f821998ee5c85d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Sun, 20 Nov 2022 21:37:46 +0300 Subject: [PATCH 2/9] Update default-parameter-values.md --- _tour/default-parameter-values.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index facaae9283..0ff1ebfec2 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -75,8 +75,8 @@ The other reason is to avoid the ambiguity that can be caused due to the existen {% tab 'Scala 2 and 3' for=default-parameter-values-5 %} ```scala mdoc:reset object A { - def func(x: Int = 34) {} - def func(y: String = "abc") {} + def func(x: Int = 34): Unit = ... + def func(y: String = "abc"): Unit = ... } ``` {% endtab %} From 9350d8b30784c10e78df88412c13c7bad9d85be1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Sun, 20 Nov 2022 21:40:49 +0300 Subject: [PATCH 3/9] Update default-parameter-values.md --- _tour/default-parameter-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index 0ff1ebfec2..7d37854431 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -73,7 +73,7 @@ The other reason is to avoid the ambiguity that can be caused due to the existen {% tabs default-parameter-values-5 %} {% tab 'Scala 2 and 3' for=default-parameter-values-5 %} -```scala mdoc:reset +```scala object A { def func(x: Int = 34): Unit = ... def func(y: String = "abc"): Unit = ... From 49be0fceebe034952476151c4194e9942bb95254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Mon, 21 Nov 2022 12:51:17 +0300 Subject: [PATCH 4/9] Update _tour/default-parameter-values.md Co-authored-by: Jamie Thompson --- _tour/default-parameter-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index 7d37854431..19cfab7138 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -65,7 +65,7 @@ public class Main { ### Default Parameters for Overloaded Methods -It's important to notice that Scala doesn't allow having two methods with default parameters and with the same name (overloaded). The reason is to keep a deterministic naming-scheme for the generated methods which return default arguments. +It's important to notice that Scala doesn't allow having two methods with default parameters and with the same name (overloaded). For example, if you write `def f(a: Int = 1)`, the compiler generates `def f$default$1 = 1`. If you have two overloads with defaults on the same parameter position, then we would need a different naming scheme. In this situation, it is preferred to keep the generated byte-code stable over multiple compiler runs. From af8ad1bbb87491963c5dab19e11ec9c2e40f8301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Mon, 21 Nov 2022 12:55:45 +0300 Subject: [PATCH 5/9] Update _tour/default-parameter-values.md Co-authored-by: Jamie Thompson --- _tour/default-parameter-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index 19cfab7138..eafe81aff8 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -73,7 +73,7 @@ The other reason is to avoid the ambiguity that can be caused due to the existen {% tabs default-parameter-values-5 %} {% tab 'Scala 2 and 3' for=default-parameter-values-5 %} -```scala +```scala mdoc:fail object A { def func(x: Int = 34): Unit = ... def func(y: String = "abc"): Unit = ... From 23cfdf62ed5da5c0567575e457c4da89083224a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Mon, 21 Nov 2022 12:55:53 +0300 Subject: [PATCH 6/9] Update _tour/default-parameter-values.md Co-authored-by: Jamie Thompson --- _tour/default-parameter-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index eafe81aff8..34e21fdac1 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -69,7 +69,7 @@ It's important to notice that Scala doesn't allow having two methods with defaul For example, if you write `def f(a: Int = 1)`, the compiler generates `def f$default$1 = 1`. If you have two overloads with defaults on the same parameter position, then we would need a different naming scheme. In this situation, it is preferred to keep the generated byte-code stable over multiple compiler runs. -The other reason is to avoid the ambiguity that can be caused due to the existence of default parameters. To illustrate the problem, let's consider the method declarations provided below: +An important reason why is to avoid the ambiguity that can be caused due to the existence of default parameters. To illustrate the problem, let's consider the method declarations provided below: {% tabs default-parameter-values-5 %} {% tab 'Scala 2 and 3' for=default-parameter-values-5 %} From b3c730aba0f0ee7a9acd347506437f746d7fa42a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Mon, 21 Nov 2022 13:01:37 +0300 Subject: [PATCH 7/9] Remove dots from scala expression --- _tour/default-parameter-values.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index 34e21fdac1..f229cbb7a6 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -75,8 +75,8 @@ An important reason why is to avoid the ambiguity that can be caused due to the {% tab 'Scala 2 and 3' for=default-parameter-values-5 %} ```scala mdoc:fail object A { - def func(x: Int = 34): Unit = ... - def func(y: String = "abc"): Unit = ... + def func(x: Int = 34): Unit + def func(y: String = "abc"): Unit } ``` {% endtab %} From cad91113a3282c6d9d38272da9ee1ae779c4c0ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Mon, 21 Nov 2022 13:02:36 +0300 Subject: [PATCH 8/9] Update the description --- _tour/default-parameter-values.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index f229cbb7a6..346ba935a3 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -65,10 +65,7 @@ public class Main { ### Default Parameters for Overloaded Methods -It's important to notice that Scala doesn't allow having two methods with default parameters and with the same name (overloaded). - -For example, if you write `def f(a: Int = 1)`, the compiler generates `def f$default$1 = 1`. If you have two overloads with defaults on the same parameter position, then we would need a different naming scheme. In this situation, it is preferred to keep the generated byte-code stable over multiple compiler runs. - +Scala doesn't allow having two methods with default parameters and with the same name (overloaded). An important reason why is to avoid the ambiguity that can be caused due to the existence of default parameters. To illustrate the problem, let's consider the method declarations provided below: {% tabs default-parameter-values-5 %} From 80ea0934f2f8916a9d151d7703d6b3feb2aa6480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karahan=20Sar=C4=B1ta=C5=9F?= <44376034+KarahanS@users.noreply.github.com> Date: Mon, 21 Nov 2022 13:05:23 +0300 Subject: [PATCH 9/9] Separate Scala 3 and Scala 2 Co-authored-by: Jamie Thompson --- _tour/default-parameter-values.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/_tour/default-parameter-values.md b/_tour/default-parameter-values.md index 346ba935a3..96b28f278a 100644 --- a/_tour/default-parameter-values.md +++ b/_tour/default-parameter-values.md @@ -68,8 +68,8 @@ public class Main { Scala doesn't allow having two methods with default parameters and with the same name (overloaded). An important reason why is to avoid the ambiguity that can be caused due to the existence of default parameters. To illustrate the problem, let's consider the method declarations provided below: -{% tabs default-parameter-values-5 %} -{% tab 'Scala 2 and 3' for=default-parameter-values-5 %} +{% tabs default-parameter-values-5 class=tabs-scala-version %} +{% tab 'Scala 2' %} ```scala mdoc:fail object A { def func(x: Int = 34): Unit @@ -77,6 +77,13 @@ object A { } ``` {% endtab %} +{% tab 'Scala 3' %} +```scala +object A: + def func(x: Int = 34): Unit + def func(y: String = "abc"): Unit +``` +{% endtab %} {% endtabs %} If we call `A.func()`, compiler cannot know whether the programmer intended to call `func(x: Int = 34)` or `func(y: String = "abc")`.