Skip to content

Commit c6a561c

Browse files
committed
Expand docs on overloading resolution
1 parent 217eda0 commit c6a561c

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

docs/docs/reference/changed-features/overload-resolution.md

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,72 @@ layout: doc-page
33
title: "Changes in Overload Resolution"
44
---
55

6-
Overload resolution in Dotty takes all argument blocks into account instead of
7-
just the first argument block.
6+
Overload resolution in Dotty improves on Scala 2 in two ways.
7+
First, it takes all argument lists into account instead of
8+
just the first argument list.
9+
Second, it can infer parameter types of function values even if they
10+
are in the first argument list.
811

12+
## Looking Beyond the First Argument List
13+
14+
Overloading resolution now can take argument lists into account when
15+
choosing among a set of overloaded alternatives.
916
For example, the following code compiles in Dotty, while it results in an
1017
ambiguous overload error in Scala2:
1118

12-
```Scala
13-
def f(x: Int)(y: String): Int = 0
14-
def f(x: Int)(y: Int): Int = 0
19+
```scala
20+
def f(x: Int)(y: String): Int = 0
21+
def f(x: Int)(y: Int): Int = 0
1522

16-
f(3)("") // ok
23+
f(3)("") // ok
1724
```
1825

1926
The following code compiles as well:
2027

21-
```Scala
22-
def g(x: Int)(y: Int)(z: Int): Int = 0
23-
def g(x: Int)(y: Int)(z: String): Int = 0
28+
```scala
29+
def g(x: Int)(y: Int)(z: Int): Int = 0
30+
def g(x: Int)(y: Int)(z: String): Int = 0
2431

25-
g(2)(3)(4) // ok
26-
g(2)(3)("") // ok
32+
g(2)(3)(4) // ok
33+
g(2)(3)("") // ok
2734
```
2835

36+
To make this work, the rules for overloading resolution in section 6.23.3 of the SLS are augmented
37+
as follows:
38+
39+
> In a situation where a function is applied to more than one argument list, if overloading
40+
resolution yields several competing alternatives when `n >= 1` parameter lists are taken
41+
into account, then resolution re-tried using `n + 1` argument lists.
42+
2943
This change is motivated by the new language feature [extension
3044
methods](../contextual/extension-methods.html), where emerges the need to do
3145
overload resolution based on additional argument blocks.
46+
47+
## Parameter Types of Function Values
48+
49+
The handling of function values with missing parameter types has been improved. We can now
50+
pass such values in the first argument list of an overloaded application, provided
51+
that the remaining parameters suffice for picking a variant of the overloaded function.
52+
For example, the following code compiles in Dotty, while it results in an
53+
missing parameter type error in Scala2:
54+
```scala
55+
def f(x: Int, f: Int => Int) = f(x)
56+
def f(x: String, f: String => String) = f(x)
57+
f("a", _.length)
58+
```
59+
To make this work, the rules for overloading resolution in section 6.23.3 of the SLS are modified
60+
as follows:
61+
62+
Replace the sentence
63+
64+
> Otherwise, let `S1,…,Sm` be the vector of types obtained by typing each argument with an undefined expected type.
65+
66+
with the following paragraph:
67+
68+
> Otherwise, let `S1,…,Sm` be the vector of known types of all argument types, where the _known type_ of an argument `E`
69+
is determined as followed:
70+
71+
- If `E` is a function value `(p_1, ..., p_n) => B` that misses some parameter types, the known type
72+
of `E` is `(S_1, ..., S_n) => ?`, where each `S_i` is the type of parameter `p_i` if it is given, or `?`
73+
otherwise. Here `?` stands for a _wildcard type_ that is compatible with every other type.
74+
- Otherwise the known type of `E` is the result of typing `E` with an undefined expected type.

0 commit comments

Comments
 (0)