@@ -3,29 +3,72 @@ layout: doc-page
3
3
title : " Changes in Overload Resolution"
4
4
---
5
5
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.
8
11
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.
9
16
For example, the following code compiles in Dotty, while it results in an
10
17
ambiguous overload error in Scala2:
11
18
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
15
22
16
- f(3 )(" " ) // ok
23
+ f(3 )(" " ) // ok
17
24
```
18
25
19
26
The following code compiles as well:
20
27
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
24
31
25
- g(2 )(3 )(4 ) // ok
26
- g(2 )(3 )(" " ) // ok
32
+ g(2 )(3 )(4 ) // ok
33
+ g(2 )(3 )(" " ) // ok
27
34
```
28
35
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
+
29
43
This change is motivated by the new language feature [ extension
30
44
methods] ( ../contextual/extension-methods.html ) , where emerges the need to do
31
45
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