You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/changed-features/overload-resolution.md
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,8 +40,8 @@ as follows:
40
40
resolution yields several competing alternatives when `n >= 1` parameter lists are taken
41
41
into account, then resolution re-tried using `n + 1` argument lists.
42
42
43
-
This change is motivated by the new language feature[extension
44
-
methods](../contextual/extension-methods.md), where emerges the need to do
43
+
This change is motivated by the new language feature
44
+
[extension methods](../contextual/extension-methods.md), where emerges the need to do
45
45
overload resolution based on additional argument blocks.
46
46
47
47
## Parameter Types of Function Values
@@ -51,12 +51,14 @@ pass such values in the first argument list of an overloaded application, provid
51
51
that the remaining parameters suffice for picking a variant of the overloaded function.
52
52
For example, the following code compiles in Scala 3, while it results in an
53
53
missing parameter type error in Scala2:
54
+
54
55
```scala
55
56
deff(x: Int, f2: Int=>Int) = f2(x)
56
57
deff(x: String, f2: String=>String) = f2(x)
57
58
f("a", _.toUpperCase)
58
59
f(2, _ *2)
59
60
```
61
+
60
62
To make this work, the rules for overloading resolution in [SLS §6.26.3](https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html#overloading-resolution) are modified
61
63
as follows:
62
64
@@ -75,11 +77,15 @@ is determined as followed:
75
77
- Otherwise the known type of `E` is the result of typing `E` with an undefined expected type.
76
78
77
79
A pattern matching closure
80
+
78
81
```scala
79
82
{ caseP1=>B1 ... caseP_n=>B_n }
80
83
````
84
+
81
85
is treated asif it was expanded to the function value
86
+
82
87
```scala
83
88
x => x match { caseP1=>B1 ... caseP_n=>B_n }
84
89
```
90
+
85
91
and is therefore also approximated with a `? => ?` type.
Copy file name to clipboardExpand all lines: docs/docs/reference/changed-features/pattern-bindings.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ From Scala 3.1 on, type checking rules will be tightened so that warnings are re
13
13
```scala
14
14
valxs:List[Any] =List(1, 2, 3)
15
15
val (x: String) :: _ = xs // error: pattern's type String is more specialized
16
-
// than the righthand side expression's type Any
16
+
// than the right-hand side expression's type Any
17
17
```
18
18
This code gives a compile-time warning in Scala 3.1 (and also in Scala 3.0 under the `-source 3.1` setting) whereas it will fail at runtime with a `ClassCastException` in Scala 2. In Scala 3.1, a pattern binding is only allowed if the pattern is _irrefutable_, that is, if the right-hand side's type conforms to the pattern's type. For instance, the following is OK:
19
19
```scala
@@ -38,7 +38,7 @@ Analogous changes apply to patterns in `for` expressions. For instance:
38
38
```scala
39
39
valelems:List[Any] =List((1, 2), "hello", (3, 4))
40
40
for (x, y) <- elems yield (y, x) // error: pattern's type (Any, Any) is more specialized
41
-
// than the righthand side expression's type Any
41
+
// than the right-hand side expression's type Any
42
42
```
43
43
This code gives a compile-time warning in Scala 3.1 whereas in Scala 2 the list `elems`
44
44
is filtered to retain only the elements of tuple type that match the pattern `(x, y)`.
Since `mkAnnotations` is `transparent`, the type of an application is the type of its righthand side, which can be a proper subtype of the declared result type `Annotations[A, T]`.
100
+
Since `mkAnnotations` is `transparent`, the type of an application is the type of its right-hand side, which can be a proper subtype of the declared result type `Annotations[A, T]`.
102
101
103
102
## Pattern-Bound Given Instances
104
103
@@ -167,5 +166,5 @@ of given instances:
167
166
168
167
- A _structural instance_ contains one or more types or constructor applications,
169
168
followed by `with` and a template body that contains member definitions of the instance.
170
-
- An _alias instance_ contains a type, followed by `=` and a righthand side expression.
169
+
- An _alias instance_ contains a type, followed by `=` and a right-hand side expression.
171
170
- An _abstract instance_ contains just the type, which is not followed by anything.
0 commit comments