From 5953f8bd55095fed97ef8cff866ab474f07b3739 Mon Sep 17 00:00:00 2001 From: odersky Date: Fri, 25 Mar 2022 11:46:19 +0100 Subject: [PATCH 1/2] Document different handling of default arguments in overloading Document different handling of default arguments in overloading resolution relative to Scala-2. Fixes #14675 --- .../changed-features/overload-resolution.md | 12 +++++++++++- tests/run/i14675.check | 1 + tests/run/i14675.scala | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/run/i14675.check create mode 100644 tests/run/i14675.scala diff --git a/docs/_docs/reference/changed-features/overload-resolution.md b/docs/_docs/reference/changed-features/overload-resolution.md index e6c929f7a2ef..23686591e06a 100644 --- a/docs/_docs/reference/changed-features/overload-resolution.md +++ b/docs/_docs/reference/changed-features/overload-resolution.md @@ -4,11 +4,12 @@ title: "Changes in Overload Resolution" movedTo: https://docs.scala-lang.org/scala3/reference/changed-features/overload-resolution.html --- -Overload resolution in Scala 3 improves on Scala 2 in two ways. +Overload resolution in Scala 3 improves on Scala 2 in three ways. First, it takes all argument lists into account instead of just the first argument list. Second, it can infer parameter types of function values even if they are in the first argument list. +Third, default arguments are no longer relevant for prioritization. ## Looking Beyond the First Argument List @@ -90,3 +91,12 @@ x => x match { case P1 => B1 ... case P_n => B_n } ``` and is therefore also approximated with a `? => ?` type. + +## Default Arguments Are No longer Relevant for Prioritization + +In Scala 2 if among several applicative alternatives one alternative had default arguments, that alternative was dropped from consideration. This has the unfortunate +side effect that adding a default parameter to a method can change render this method +invisible in overloaded calls. + +Scala 3 drops this distinction. Methods with default parameters are not treated +to have lower priority than other methods. diff --git a/tests/run/i14675.check b/tests/run/i14675.check new file mode 100644 index 000000000000..d00491fd7e5b --- /dev/null +++ b/tests/run/i14675.check @@ -0,0 +1 @@ +1 diff --git a/tests/run/i14675.scala b/tests/run/i14675.scala new file mode 100644 index 000000000000..a776fe6d05ef --- /dev/null +++ b/tests/run/i14675.scala @@ -0,0 +1,4 @@ +def f(x: Int = 0): Int = 1 +def f(x: Int*): Int = 2 + +@main def Test = println(f()) From bf72b7da4abff270137ae10e05dce8530acd8509 Mon Sep 17 00:00:00 2001 From: odersky Date: Fri, 25 Mar 2022 19:46:29 +0100 Subject: [PATCH 2/2] Update docs/_docs/reference/changed-features/overload-resolution.md --- docs/_docs/reference/changed-features/overload-resolution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_docs/reference/changed-features/overload-resolution.md b/docs/_docs/reference/changed-features/overload-resolution.md index 23686591e06a..acdf3e96a381 100644 --- a/docs/_docs/reference/changed-features/overload-resolution.md +++ b/docs/_docs/reference/changed-features/overload-resolution.md @@ -95,7 +95,7 @@ and is therefore also approximated with a `? => ?` type. ## Default Arguments Are No longer Relevant for Prioritization In Scala 2 if among several applicative alternatives one alternative had default arguments, that alternative was dropped from consideration. This has the unfortunate -side effect that adding a default parameter to a method can change render this method +side effect that adding a default to a parameter of a method can render this method invisible in overloaded calls. Scala 3 drops this distinction. Methods with default parameters are not treated