Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 84ff4c8

Browse files
authoredJan 4, 2019
Document assignment destructuring
1 parent 8b189ec commit 84ff4c8

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed
 

Diff for: ‎doc/src/manual/functions.md

+27-5
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,34 @@ julia> x.a
278278
Named tuples are very similar to tuples, except that fields can additionally be accessed by name
279279
using dot syntax (`x.a`).
280280

281-
## Multiple Return Values
281+
## Assignment destructuring and Multiple Return Values
282282

283-
In Julia, one returns a tuple of values to simulate returning multiple values. However, tuples
284-
can be created and destructured without needing parentheses, thereby providing an illusion that
285-
multiple values are being returned, rather than a single tuple value. For example, the following
286-
function returns a pair of values:
283+
A comma-separated list of variables (optionally wrapped in parentheses) can appear on the left
284+
side of an assignment: the value on the right side is _destructured_ by iterating over and
285+
assigned to each variable in turn:
286+
287+
```jldoctest
288+
julia> (a,b,c) = 1:3
289+
1:3
290+
291+
julia> b
292+
2
293+
```
294+
295+
The iterator needs to be at least as long as the number of variables, and any excess elements
296+
are ignored. Additionally a single underscore `_` (which is an otherwise invalid variable name)
297+
can be used on the left to avoid assigning specific elements:
298+
299+
```jldoctest
300+
julia> _,_,_,d = 1:10
301+
1:10
302+
303+
julia> d
304+
4
305+
```
306+
307+
This can be used to simulate returning multiple values from functions by returning a tuple or
308+
other iterable value. For example, the following function returns a pair of values:
287309

288310
```jldoctest foofunc
289311
julia> function foo(a,b)

0 commit comments

Comments
 (0)
Please sign in to comment.