Skip to content

Commit 825d9c5

Browse files
committed
Update documentation for parameter untupling
- `TupleN` instead of `ProductN` - Make two steps in type checking more clear
1 parent 6d9e101 commit 825d9c5

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

docs/docs/reference/other-new-features/parameter-untupling-spec.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: doc-page
33
title: "Parameter Untupling - More Details"
44
---
55

6-
### Motivation
6+
## Motivation
77

88
Say you have a list of pairs
99

@@ -35,31 +35,35 @@ xs.map(_ + _)
3535

3636
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`.
3737

38-
### Type Checking
38+
## Type Checking
3939

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:
4141

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` will conform to the function type `TupleN[T1, ..., Tn] => R`.
4550

4651
A type `Ti` fits a parameter `pi` if one of the following two cases is `true`:
4752

4853
* `pi` comes without a type, i.e. it is a simple identifier or `_`.
4954
* `pi` is of the form `x: Ui` or `_: Ui` and `Ti <: Ui`.
5055

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.
5357

54-
#### Term adaptation
58+
### Term adaptation
5559

5660
If the function
5761

5862
```scala
59-
(p1: T1, ..., pn: Tn) => e
63+
(p1, ..., pn) => e
6064
```
6165

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
6367

6468
```scala
6569
(x: TupleN[T1, ..., Tn]) =>
@@ -69,19 +73,15 @@ is typed as `ProductN[T1, ..., Tn] => Te`, then it will be transformed to
6973
e
7074
```
7175

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
7878

7979
Code like this could not be written before, hence the new notation would not be ambiguous after adoption.
8080

8181
Though it is possible that someone has written an implicit conversion form `(T1, ..., Tn) => R` to `TupleN[T1, ..., Tn] => R`
8282
for some `n`. This change could be detected and fixed by [`Scalafix`](https://scalacenter.github.io/scalafix/). Furthermore, such conversion would probably
8383
be doing the same translation (semantically) but in a less efficient way.
8484

85-
### Reference
85+
## Reference
8686

8787
For more information, see [Issue #897](https://github.com/lampepfl/dotty/issues/897).

0 commit comments

Comments
 (0)