# New Destructuring Syntax

**URL:** <https://ziggit.dev/t/new-destructuring-syntax/1757>\
**Category:** News\
**Tags:** language\
**Created:** [September 16, 2023, 2:06am UTC](https://ziggit.dev/t/new-destructuring-syntax/1757 "2023-09-16T02:06:49Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![zraineri](https://ziggit.dev/user_avatar/ziggit.dev/zraineri/32/755_2.png) [@zraineri](https://ziggit.dev/u/zraineri)\
**Post date:** [September 16, 2023, 2:06am UTC](https://ziggit.dev/t/new-destructuring-syntax/1757/1 "2023-09-16T02:06:49Z")

</div>

It looks like @mlugg has done in incredible job at implementing a destructuring syntax into the compiler

**Pull Request:**

> <https://github.com/ziglang/zig/pull/17156>
>
> There are a few unrelated minor changes in the first couple of commits.
> 
> In ge…neral, see commit messages for details.

**Proposal:**

> <https://github.com/ziglang/zig/issues/498>
>
> \[Latest Proposal\](https://github.com/ziglang/zig/issues/498#issuecomment-4405213…57)
> 
> \----
> 
> This proposal is an alternative to the rejected multiple expression values proposal (#83). It affects inline assembly improvements (#215). It depends on or at least is related to \[my comment\](https://github.com/zig-lang/zig/issues/346#issuecomment-329266675) in #346.
> 
> \* Add ability for functions to have multiple return values.
> 
> \`\`\`zig
> fn div(numerator: i32, denominator: i32) -\> i32, i32 {
> return numerator / denominator, numerator % denominator;
> }
> \`\`\`\`
> 
> \* If you want an error, it's recommended to use a struct:
> 
> \`\`\`zig
> error DivByZero;
> const DivResult = struct {quotient: i32, remainder: i32 };
> fn div(numerator: i32, denominator: i32) -\> %DivResult {
> if (denominator == 0) return error.DivByZero;
> return DivResult {
> .quotient = numerator / denominator,
> .remainder = numerator % denominator,
> };
> }
> \`\`\`
> 
> \* \`return\` statements can have multiple return values:
> 
> \`\`\`zig
> fn foo(condition: bool) {
> const x, const y = div(3, 1);
> const a, const b = if (condition) {
> return :this false, 1234;
> } else {
> return :this true, 5678;
> };
> }
> \`\`\`
> 
> This is not general-purpose tuples. This is multiple assignment and multiple return values.
> 
> Real Actual Use Case: https://github.com/zig-lang/zig/blob/cba4a9ad4a149766c650e3f3d71435cef14867a3/std/os/child\_process.zig#L237-L246

**To quote his commit:**

> <https://github.com/ziglang/zig/pull/17156/commits/88f5315ddfc6eaf3e28433504ec046fb3252db7c>
>
> This change implements the following syntax into the compiler:
> 
> \`\`\`zig
> const x: …u32, var y, foo.bar = .{ 1, 2, 3 };
> \`\`\`
> 
> A destructure expression may only appear within a block (i.e. not at
> comtainer scope). The LHS consists of a sequence of comma-separated var
> decls and/or lvalue expressions. The RHS is a normal expression.
> 
> A new result location type, \`destructure\`, is used, which contains
> result pointers for each component of the destructure. This means that
> when the RHS is a more complicated expression, peer type resolution is
> not used: each result value is individually destructured and written to
> the result pointers. RLS is always used for destructure expressions,
> meaning every \`const\` on the LHS of such an expression creates a true
> stack allocation.
> 
> Aside from anonymous array literals, Sema is capable of destructuring
> the following types:
> \* Tuples
> \* Arrays
> \* Vectors
> 
> A destructure may be prefixed with the \`comptime\` keyword, in which case
> the entire destructure is evaluated at comptime: this means all \`var\`s
> in the LHS are \`comptime var\`s, every lvalue expression is evaluated at
> comptime, and the RHS is evaluated at comptime. If every LHS is a
> \`const\`, this is not allowed: as with single declarations, the user
> should instead mark the RHS as \`comptime\`.
> 
> There are a few subtleties in the grammar changes here. For one thing,
> if every LHS is an lvalue expression (rather than a var decl), a
> destructure is considered an expression. This makes, for instance,
> \`if (cond) x, y = .{ 1, 2 };\` valid Zig code. A destructure is allowed
> in almost every context where a standard assignment expression is
> permitted. The exception is \`switch\` prongs, which cannot be
> destructures as the comma is ambiguous with the end of the prong.
> 
> A follow-up commit will begin utilizing this syntax in the Zig compiler.
> 
> Resolves: #498

> This change implements the following syntax into the compiler:
> 
> ```zig
> const x: u32, var y, foo.bar = .{ 1, 2, 3 };
> 
> ```
> 
> A destructure expression may only appear within a block (i.e. not at  
> comtainer scope). The LHS consists of a sequence of comma-separated var  
> decls and/or lvalue expressions. The RHS is a normal expression.
> 
> A new result location type, `destructure`, is used, which contains  
> result pointers for each component of the destructure. This means that  
> when the RHS is a more complicated expression, peer type resolution is  
> not used: each result value is individually destructured and written to  
> the result pointers. RLS is always used for destructure expressions,  
> meaning every `const` on the LHS of such an expression creates a true  
> stack allocation.
> 
> Aside from anonymous array literals, Sema is capable of destructuring  
> the following types:
> 
> - Tuples
> - Arrays
> - Vectors
> 
> A destructure may be prefixed with the `comptime` keyword, in which case  
> the entire destructure is evaluated at comptime: this means all `var`s  
> in the LHS are `comptime var`s, every lvalue expression is evaluated at  
> comptime, and the RHS is evaluated at comptime. If every LHS is a  
> `const`, this is not allowed: as with single declarations, the user  
> should instead mark the RHS as `comptime`.
> 
> There are a few subtleties in the grammar changes here. For one thing,  
> if every LHS is an lvalue expression (rather than a var decl), a  
> destructure is considered an expression. This makes, for instance,  
> `if (cond) x, y = .{ 1, 2 };` valid Zig code. A destructure is allowed  
> in almost every context where a standard assignment expression is  
> permitted. The exception is `switch` prongs, which cannot be  
> destructures as the comma is ambiguous with the end of the prong.
> 
> A follow-up commit will begin utilizing this syntax in the Zig compiler.

---

_[View the full topic](https://ziggit.dev/t/new-destructuring-syntax/1757)._
