# Property destructuring

**URL:** <https://discourse.julialang.org/t/property-destructuring/101550>\
**Category:** New to Julia\
**Tags:** destructuring\
**Created:** [July 12, 2023, 8:24pm UTC](https://discourse.julialang.org/t/property-destructuring/101550 "2023-07-12T20:24:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ralf\_Dietrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralf_dietrich/32/5643_2.png) [@Ralf\_Dietrich](https://discourse.julialang.org/u/Ralf_Dietrich)\
**Post date:** [July 12, 2023, 8:24pm UTC](https://discourse.julialang.org/t/property-destructuring/101550/1 "2023-07-12T20:24:18Z")

</div>

Unter [Property destructoring](https://docs.julialang.org/en/v1/manual/functions/#Property-destructuring) I found a simple example for property destructuring.

```julia
(; b, a) = (a=1, b=2, c=3)

```

I understand how it works, but it is not clear to me where you can use this practically.

Can someone show me a small executable example code that opens up the practical application for me?

Many thanks in advance

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 12, 2023, 8:30pm UTC](https://discourse.julialang.org/t/property-destructuring/101550/2 "2023-07-12T20:30:19Z")

</div>

It’s useful as soon as the named tuple is a variable instead of a literal.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 12, 2023, 8:31pm UTC](https://discourse.julialang.org/t/property-destructuring/101550/3 "2023-07-12T20:31:32Z")

</div>

Some examples in this related [thread](https://discourse.julialang.org/t/destructuring-syntax-in-julia-1-7/68236).

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 12, 2023, 8:39pm UTC](https://discourse.julialang.org/t/property-destructuring/101550/4 "2023-07-12T20:39:26Z")

</div>

Here is an example - imagine that `tpl` is returned by some function and is not a literal.

```julia
tpl = (a=1, b=2, c=3)

a = tpl.a
b = tpl.b

# or

a, b = tpl.a, tpl.b

# or using destructuring

(; a, b) = tpl

```

Now, we can agree that `(; a, b) = tpl` saves a few keystrokes. And if you actually want to extract more fields (imagine `(; a, b, c, d, e) = x`), then it becomes even more obvious that it saves some time.

Also, it works even if your right side is a `struct` (even more, it works for any type that has the `getproperty` method defined).

This is just syntactic sugar to make things more convenient for the developer. The other forms from my example are also OK.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 12, 2023, 8:49pm UTC](https://discourse.julialang.org/t/property-destructuring/101550/5 "2023-07-12T20:49:19Z")

</div>

Here is a sketch of some code I’m currently working on that uses destructuring for its convenience:

```julia
...
lens = (; f1, α, β, γ, y3, sinθ, ϵᵣ)
opts = (;
    ap_split = 2, 
    ap_point = 0.0,
    ap_compensate = false,
    bp_split = 2,
    bp_point = 0.0,
    bp_compensate = true,
    ap_apex = 0.046,
    bp_apex = 0.046,
    ap_length=0.423,
    bp_length=0.423,
    λ)

lens = RotmanLens.rl_solve(lens)
lens = RotmanLens.port_apertures(lens, opts) 
lens = RotmanLens.rl_excite(lens, opts)
...

```

The functions are set up like this:

```julia
function rl_solve(lens::NamedTuple)
    (; f1, α, β, γ, y3, sinθ, ϵᵣ) = lens # Deconstruct fields
    xya = similar(y3, SV2)
    xyb = similar(sinθ, SV2)
    w = similar(y3)
    ... # Compute entries of xya, xyb, w
    return merge(lens, (; xya, xyb, w))
end

```

Each function destructures whatever fields are needed from `lens::NamedTuple` and merges in the part that is computed locally. It is much more convenient than tracking many inputs and outputs.

---

<div class="post-metadata">

**Author:** ![Ralf\_Dietrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralf_dietrich/32/5643_2.png) [@Ralf\_Dietrich](https://discourse.julialang.org/u/Ralf_Dietrich)\
**Post date:** [July 12, 2023, 9:46pm UTC](https://discourse.julialang.org/t/property-destructuring/101550/6 "2023-07-12T21:46:06Z")

</div>

Thank you for the very good help to all of you. I am amazed
