Skip to content

Commit ad7e59a

Browse files
Kenograhamas
andauthored
Document destructuring syntax in anonymous function argument (#39024)
When destructuring a single tuple in an anonymous function argument, you have to include an extra comma to force a 1-tuple. Otherwise ((x,y)) collapses to (x,y). This is not the case for named functions. Co-authored-by: Graham Smith <grahamas@gmail.com>
1 parent 345ce78 commit ad7e59a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

doc/src/manual/functions.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,15 @@ julia> gap(minmax(10, 2))
395395
Notice the extra set of parentheses in the definition of `gap`. Without those, `gap`
396396
would be a two-argument function, and this example would not work.
397397

398+
For anonymous functions, destructuring a single tuple requires an extra comma:
399+
400+
```
401+
julia> map(((x,y),) -> x + y, [(1,2), (3,4)])
402+
2-element Array{Int64,1}:
403+
3
404+
7
405+
```
406+
398407
## Varargs Functions
399408

400409
It is often convenient to be able to write functions taking an arbitrary number of arguments.
@@ -681,8 +690,8 @@ end
681690
```
682691

683692
The `do x` syntax creates an anonymous function with argument `x` and passes it as the first argument
684-
to [`map`](@ref). Similarly, `do a,b` would create a two-argument anonymous function, and a
685-
plain `do` would declare that what follows is an anonymous function of the form `() -> ...`.
693+
to [`map`](@ref). Similarly, `do a,b` would create a two-argument anonymous function. Note that `do (a,b)` would create a one-argument anonymous function,
694+
whose argument is a tuple to be deconstructed. A plain `do` would declare that what follows is an anonymous function of the form `() -> ...`.
686695

687696
How these arguments are initialized depends on the "outer" function; here, [`map`](@ref) will
688697
sequentially set `x` to `A`, `B`, `C`, calling the anonymous function on each, just as would happen

0 commit comments

Comments
 (0)