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/tupled-function.md
+20-3Lines changed: 20 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Tupled Function
8
8
9
9
With functions bounded to arities up to 22 it was possible to generalize some operation on all function types using overloading.
10
10
Now that we have functions and tuples generalized to [arities above 22](https://dotty.epfl.ch/docs/reference/dropped-features/limit22.html) overloading is not an option anymore.
11
-
The type class `TupleFunction` provides a way to abstract directly over functions of any arity converting it to an equivalent function that receives all arguments in a single tuple.
11
+
The type class `TupleFunction` provides a way to abstract directly over a function of any arity converting it to an equivalent function that receives all arguments in a single tuple.
12
12
13
13
```scala
14
14
/** Type class relating a `FunctionN[..., R]` with an equvalent tupled function `Function1[TupleN[...], R]`
@@ -19,6 +19,7 @@ The type class `TupleFunction` provides a way to abstract directly over function
19
19
@implicitNotFound("${F} cannot be tupled as ${G}")
20
20
sealedtraitTupledFunction[F, G] {
21
21
deftuple(f: F):G
22
+
defuntuple(g: G):F
22
23
}
23
24
```
24
25
@@ -27,11 +28,12 @@ The compiler will synthesize an instance of `TupledFunction[F, G]` if:
27
28
*`F` is a function type of arity `N`
28
29
*`G` is a function with a single tuple argument of size `N` and it's types are equal to the arguments of `F`
29
30
* The return type of `F` is equal to the return type of `G`
30
-
*`F` and `G` are the same kind of functions (`given` arguments or not)
31
+
*`F` and `G` are the same kind of function (both are `(...) => R` or both are `given (...) => R`)
32
+
* If only one of `F` or `G` is instantiated the second one is inferred.
31
33
32
34
Examples
33
35
--------
34
-
`TupledFunction` can be used to generalize the `Tuple2.tupled`, ... `Tuple22.tupled`method to functions of any arities ([full example](https://github.com/lampepfl/dotty/tests/run/tupled-function-tupled.scala))
36
+
`TupledFunction` can be used to generalize the `Function1.tupled`, ... `Function22.tupled`methods to functions of any arities ([full example](https://github.com/lampepfl/dotty/tests/run/tupled-function-tupled.scala))
35
37
36
38
```scala
37
39
/** Creates a tupled version of this function: instead of N arguments,
`TupledFunction` can be used to generalize the `Function.untupled` methods to functions of any arities ([full example](https://github.com/lampepfl/dotty/tests/run/tupled-function-untupled.scala))
50
+
51
+
```scala
52
+
/** Creates an untupled version of this function: instead of single [[scala.Tuple]] argument,
53
+
* it accepts a N arguments.
54
+
*
55
+
* This is a generalization of [[scala.Function.untupled]] that work on functions of any arity
56
+
*
57
+
* @tparamF the function type
58
+
* @tparamArgs the tuple type with the same types as the function arguments of F
`TupledFunction` can also be used to generalize the [`Tuple1.compose`](https://github.com/lampepfl/dotty/tests/run/tupled-function-compose.scala) and [`Tuple1.andThen`](https://github.com/lampepfl/dotty/tests/run/tupled-function-andThen.scala) methods to compose functions of larger arities and with functions that return tuples.
0 commit comments