File tree 1 file changed +27
-5
lines changed
1 file changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -278,12 +278,34 @@ julia> x.a
278
278
Named tuples are very similar to tuples, except that fields can additionally be accessed by name
279
279
using dot syntax (` x.a ` ).
280
280
281
- ## Multiple Return Values
281
+ ## Assignment destructuring and Multiple Return Values
282
282
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:
287
309
288
310
``` jldoctest foofunc
289
311
julia> function foo(a,b)
You can’t perform that action at this time.
0 commit comments