Skip to content

Add subsection "Default Parameters for Overloaded Methods" #2646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 21, 2022
Merged
25 changes: 25 additions & 0 deletions _tour/default-parameter-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,28 @@ public class Main {
```
{% endtab %}
{% endtabs %}

### Default Parameters for Overloaded Methods

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 class=tabs-scala-version %}
{% tab 'Scala 2' %}
```scala mdoc:fail
object A {
def func(x: Int = 34): Unit
def func(y: String = "abc"): Unit
}
```
{% 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")`.