# Destructuring syntax in julia 1.7

**URL:** <https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236>\
**Category:** Internals & Design\
**Tags:** destructuring\
**Created:** [September 15, 2021, 11:01pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236 "2021-09-15T23:01:36Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![xfcjscn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xfcjscn/32/19613_2.png) [@xfcjscn](https://discourse.julialang.org/u/xfcjscn)\
**Post date:** [October 13, 2021, 5:06am UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/22 "2021-10-13T05:06:50Z")

</div>

To me, `(a = 1, b=2)` is literal, `(;a, b)` or `(; :a =>1 , :b => 2)` is kinda non-literal. And a bit weird to have `;` as prefix. This syntax is also not mathematical.  
This syntax seems out of vacuum, not from mathematics.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 13, 2021, 7:49am UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/23 "2021-10-13T07:49:56Z")

</div>

`struct`s are not from mathematics either. I don’t see the problem.

To me, the “non-literality” of `(; a, b)` is _why_ this works. Just like in function arguments, where the semicolon signals “ordered arguments end here, from now on only specified, ‘extra’ information”, it means “this tuple is not a normal, ordered, tuple”.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 13, 2021, 12:03pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/24 "2021-10-13T12:03:13Z")

</div>

The semicolon is used consistently to mark “here begin keywords” throughout the language and have since Julia 0.2 (IIRC), so it certainly doesn’t come out of nowhere.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [October 13, 2021, 12:29pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/25 "2021-10-13T12:29:14Z")

</div>

It’s even required by YASGuide and BlueStyle.

[YASGuide](https://github.com/jrevels/YASGuide)

> In argument lists, always separate positional arguments from keyword arguments using a semicolon ( `;` ).

[BlueStyle](https://github.com/invenia/BlueStyle#keyword-arguments)

> When calling a function always separate your keyword arguments from your positional arguments with a semicolon. This avoids mistakes in ambiguous cases (such as splatting a `Dict` ).

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [October 14, 2021, 6:41am UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/26 "2021-10-14T06:41:17Z")

</div>

> [@StefanKarpinski](#):
>
> The semicolon is used consistently to mark “here begin keywords” throughout the language and have since Julia 0.2 (IIRC), so it certainly doesn’t come out of nowhere.

It doesn’t come out of nowhere for a seasoned Julia programmer, but I imagine it is fairly surprising for people coming from most other languages. Which is a pity, because IMHO Julia does very well on that front otherwise.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 14, 2021, 8:01am UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/27 "2021-10-14T08:01:14Z")

</div>

You don’t have to be _that_ seasoned. Day three of reading the manual, tops.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [October 14, 2021, 8:37am UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/28 "2021-10-14T08:37:10Z")

</div>

That’s assuming you are in the privileged position where you can set aside three days just to read the manual of yet another programming language. My point was that if you know how to program in any language, then you probably can read most Julia code without referring to the manual at all. The `(; foo) = bar` syntax is a major exception to this rule, and it’s one which is also potentially quite hard to google.

---

<div class="post-metadata">

**Author:** ![tsela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsela/32/52174_2.png) [@tsela](https://discourse.julialang.org/u/tsela)\
**Post date:** [October 14, 2021, 9:02am UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/29 "2021-10-14T09:02:37Z")

</div>

> [@ettersi](#):
>
> My point was that if you know how to program in any language, then you should be able to read most Julia code without referring to the manual at all.

I’m sorry, but why should Julia satisfy such a ridiculous requirement? Do you really think there’s a language out there that does? Do you really think someone who’s only ever done Common Lisp would be able to read most Python code without a manual? Or that someone who only knows Python would never need a manual to read Java?  
Julia is its own language, and is under no obligation to be readable without a manual by someone “who knows to program in another language”. As it is, Julia is very readable already, and moreover very consistent in its syntax, and you can’t realistically expect more of a programming language.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [October 14, 2021, 9:28am UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/30 "2021-10-14T09:28:04Z")

</div>

I agree on the difficulties that `;` poses for beginners. But at the same time I think this:

> [@ettersi](#):
>
> if you know how to program in any language, then you should be able to read most Julia code without referring to the manual at all

is asking too much. A big part of what makes Julia appealing is its syntax being more expressive than say Matlab or Python. And you can hardly have both “more expressive syntax” and “knowing Matlab/Python is enough to read the code”…

I mean even basic array literals are un-understandable without reading the manual!

```julia
A = 1:3
B = [A A]
C = [A, 5:9]
D = [A; 5:9]

```

Maybe a Matlab/Python user _thinks_ they understand this code, but they’re probably wrong. They’re also unlikely to guess what

```julia
:x

```

means unless they happen to know Ruby or another language with symbols.

Then there’s broadcasting:

```julia
x = 0:0.1:pi
A = cos.(x) .+ rand.()

```

Good luck understanding this without reading about loop fusion…

There’s also chained comparisons:

```julia
1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5

```

Or the convention-based difference between

```julia
filter(...)

# and

filter!(...)

```

and probably many more examples…

I’m personally happy Julia offers some pleasant, original (or at least less common) syntax rather than a rehash of 2-3 dominant languages 🙂

---

<div class="post-metadata">

**Author:** ![xfcjscn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xfcjscn/32/19613_2.png) [@xfcjscn](https://discourse.julialang.org/u/xfcjscn)\
**Post date:** [October 14, 2021, 9:53am UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/31 "2021-10-14T09:53:37Z")

</div>

Thanks for all the explanations. I can get the syntax design via take `;` as separator not prefix.  
While as separator it surely make sense.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [October 14, 2021, 2:58pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/32 "2021-10-14T14:58:28Z")

</div>

Sorry, poor choice of words on my end. When I said “you should be able to read Julia without a manual”, I meant “you probably would be able to do that”. The point that I was trying to make is exactly the one you are raising, which is that Julia is very readable. Thus `(;foo) = bar` is a bit of an odd one out.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [October 14, 2021, 3:19pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/33 "2021-10-14T15:19:56Z")

</div>

I agree with you that there are some features of Julia which you will not be able to make sense of without a manual, but for most of them it’s hard to think of a better way. Personally, I’m not convinced `(;foo) = bar` falls into this category. I’d argue the old-style `@unpack foo = bar` is potentially easier to grasp, and if not then at least there’s the obvious `?@unpack` to get help.

Having said that, I of course understand that many people way smarter than myself must have thought very carefully about why `(;foo) = bar` is better than `@unpack foo = bar`, and I imagine I will come to agree with them sooner or later. However, I’m not there yet, and I assume others aren’t either.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 14, 2021, 3:46pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/34 "2021-10-14T15:46:33Z")

</div>

This is analogous to user asking, “what is `a[3]`?”, and very few would think having `getindex(a, 3)` everywhere is a good idea.

```julia
julia> Meta.@lower (;re) = a
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = Base.getproperty(a, :re)
│ re = %1
└── return a
))))

```

in general, `?` doesn’t help with syntax question, but `@lower` does

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [October 14, 2021, 4:17pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/35 "2021-10-14T16:17:35Z")

</div>

If beginners don’t know about this syntax, they’re not very likely to know about `Meta.@lower` either, or how to read it

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 14, 2021, 4:19pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/36 "2021-10-14T16:19:15Z")

</div>

well, but that’s like saying user don’t know how to use `>?`, it’s one-time and universal enough I won’t worry about it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 14, 2021, 5:13pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/37 "2021-10-14T17:13:41Z")

</div>

> [@ettersi](#):
>
> Thus `(;foo) = bar` is a bit of an odd one out.

Is it really that much more surprising or ‘odd’ than slurp/splat syntax or destructuring of general iterables?

(For the record, I’m not a big fan of this syntax either, but not because you have to look it up in the manual.)

---

<div class="post-metadata">

**Author:** ![Glen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glen/32/3583_2.png) [@Glen](https://discourse.julialang.org/u/Glen)\
**Post date:** [October 14, 2021, 5:51pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/38 "2021-10-14T17:51:52Z")

</div>

Could the help system be able to give nice help for something like a `(;foo) = bar` expression?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 14, 2021, 6:30pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/39 "2021-10-14T18:30:15Z")

</div>

It’s a slightly weird syntax, but it’s a useful feature and this is the natural syntax for it. How did we get here?

**Methods definitions**

1. Positional args: `f(x, y)`
2. Positional and keyword args with defaults: `f(x, y; a=1, b=2)`
3. Positional and keyword args without defaults: `f(x, y; a, b)`
4. Keyword args only with defaults: `f(; a=1, b=2)`
5. Keyword args only without defaults: `f(; a, b)`

**Tuple syntax**

1. Construction, like 1 without the function: `(x, y)`
2. Destructuring, like 7 but left of an assignment: `(x, y) = ...`

**Named tuple syntax**

1. Construction with explicit values, like 4 without the function: `(; a=1, b=2)`
2. Construction with implicit values, like 5 without the function: `(; a, b)`
3. Destructuring, like 10 but left of assignment: `(; a, b) = ...`

The only thing that’s new in Julia 1.7 is the last item—named tuple destructuring. Once you see it in the context of all these other syntaxes, perhaps it becomes clear why it’s the only syntax choice that fits in with all the rest.

---

<div class="post-metadata">

**Author:** ![dalejordan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dalejordan/32/2196_2.png) [@dalejordan](https://discourse.julialang.org/u/dalejordan)\
**Post date:** [October 14, 2021, 8:00pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/40 "2021-10-14T20:00:56Z")

</div>

Might I suggest a section of the manual devoted to the concept of destructuring and examples of these various syntaxes?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 14, 2021, 8:22pm UTC](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236/41 "2021-10-14T20:22:26Z")

</div>

That’s a great idea and would be an excellent doc contribution if someone cares to take a crack at it.

[Previous page](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236.md?page=1)

[Next page](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236.md?page=3)
