Skip to content

Commit 5e209f5

Browse files
committed
Update docs
1 parent 9f02e64 commit 5e209f5

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

docs/docs/reference/other-new-features/tupled-function.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Tupled Function
88

99
With functions bounded to arities up to 22 it was possible to generalize some operation on all function types using overloading.
1010
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.
1212

1313
```scala
1414
/** 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
1919
@implicitNotFound("${F} cannot be tupled as ${G}")
2020
sealed trait TupledFunction[F, G] {
2121
def tuple(f: F): G
22+
def untuple(g: G): F
2223
}
2324
```
2425

@@ -27,11 +28,12 @@ The compiler will synthesize an instance of `TupledFunction[F, G]` if:
2728
* `F` is a function type of arity `N`
2829
* `G` is a function with a single tuple argument of size `N` and it's types are equal to the arguments of `F`
2930
* 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.
3133

3234
Examples
3335
--------
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))
3537

3638
```scala
3739
/** Creates a tupled version of this function: instead of N arguments,
@@ -44,6 +46,21 @@ Examples
4446
def (f: F) tupled[F, Args <: Tuple, R] given (tf: TupledFunction[F, Args => R]): Args => R = tf.tuple(f)
4547
```
4648

49+
`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+
* @tparam F the function type
58+
* @tparam Args the tuple type with the same types as the function arguments of F
59+
* @tparam R the return type of F
60+
*/
61+
def (f: Args => R) untupled[F, Args <: Tuple, R] given (tf: TupledFunction[F, Args => R]): F = tf.untuple(f)
62+
```
63+
4764
`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.
4865

4966
```scala

0 commit comments

Comments
 (0)