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/other-new-features/parameter-untupling-spec.md
+18-18Lines changed: 18 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ layout: doc-page
3
3
title: "Parameter Untupling - More Details"
4
4
---
5
5
6
-
###Motivation
6
+
## Motivation
7
7
8
8
Say you have a list of pairs
9
9
@@ -35,31 +35,35 @@ xs.map(_ + _)
35
35
36
36
Generally, a function value with `n > 1` parameters can be converted to a function with tupled arguments if the expected type is a unary function type of the form `((T_1, ..., T_n)) => U`.
37
37
38
-
###Type Checking
38
+
## Type Checking
39
39
40
-
Let a function `f` of the form `(p1, ..., pn) => e` for `n != 1`, parameters `p1, ..., pn`, and an expression `e`.
40
+
The type checking happens in two steps:
41
41
42
-
If the expected type of `f` is a fully defined function type or SAM-type that has a
43
-
single parameter of a subtype of `ProductN[T1, ..., Tn]`, where each type `Ti` fits the corresponding
44
-
parameter `pi`. Then `f` will conform to the function type `ProductN[T1, ..., Tn] => R`.
42
+
1. Check whether parameter untupling is feasible
43
+
2. Adapt the function and type check it
44
+
45
+
### Feasibility Check
46
+
47
+
Suppose a function `f` of the form `(p1, ..., pn) => e` (where `n > 1`), with `p1, ..., pn` as parameters and `e` as function body.
48
+
49
+
If the expected type for checking `f` is a fully defined function type of the form `TupleN[T1, ..., Tn] => R` (or an equivalent SAM-type), where each type `Ti` fits the corresponding parameter `pi`. Then `f` is feasible for parameter untupling with the expected type `TupleN[T1, ..., Tn] => R`.
45
50
46
51
A type `Ti` fits a parameter `pi` if one of the following two cases is `true`:
47
52
48
53
*`pi` comes without a type, i.e. it is a simple identifier or `_`.
49
54
*`pi` is of the form `x: Ui` or `_: Ui` and `Ti <: Ui`.
50
55
51
-
Auto-tupling composes with eta-expansion. That is an n-ary function generated by eta-expansion
52
-
can in turn be adapted to the expected type with auto-tupling.
56
+
Parameter untupling composes with eta-expansion. That is, an n-ary function generated by eta-expansion can in turn be adapted to the expected type with parameter untupling.
53
57
54
-
####Term adaptation
58
+
### Term adaptation
55
59
56
60
If the function
57
61
58
62
```scala
59
-
(p1: T1, ..., pn: Tn) => e
63
+
(p1, ..., pn) => e
60
64
```
61
65
62
-
is typed as `ProductN[T1, ..., Tn] => Te`, then it will be transformed to
66
+
is feasible for parameter untupling with the expected type `TupleN[T1, ..., Tn] => Te`, then continue to type check the following adapted function
63
67
64
68
```scala
65
69
(x: TupleN[T1, ..., Tn]) =>
@@ -69,19 +73,15 @@ is typed as `ProductN[T1, ..., Tn] => Te`, then it will be transformed to
69
73
e
70
74
```
71
75
72
-
##### Generic tuples
73
-
74
-
If we come to support generic tuples, which provide the possibility of having tuples/functions of arities larger than 22 we would need to additionally support generic tuples of the form `T1 *: T2 *: ...`.
75
-
Translation of such a tuples would use the `apply` method on the tuple to access the elements instead of the `_N` methods of `Product`.
76
-
77
-
### Migration
76
+
with the same expected type.
77
+
## Migration
78
78
79
79
Code like this could not be written before, hence the new notation would not be ambiguous after adoption.
80
80
81
81
Though it is possible that someone has written an implicit conversion form `(T1, ..., Tn) => R` to `TupleN[T1, ..., Tn] => R`
82
82
for some `n`. This change could be detected and fixed by [`Scalafix`](https://scalacenter.github.io/scalafix/). Furthermore, such conversion would probably
83
83
be doing the same translation (semantically) but in a less efficient way.
84
84
85
-
###Reference
85
+
## Reference
86
86
87
87
For more information, see [Issue #897](https://github.com/lampepfl/dotty/issues/897).
0 commit comments