# Structure binding in Julia?

**URL:** <https://discourse.julialang.org/t/structure-binding-in-julia/90931>\
**Category:** General Usage\
**Created:** [November 28, 2022, 3:21am UTC](https://discourse.julialang.org/t/structure-binding-in-julia/90931 "2022-11-28T03:21:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gtgt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gtgt/32/46932_2.png) [@gtgt](https://discourse.julialang.org/u/gtgt)\
**Post date:** [November 28, 2022, 3:21am UTC](https://discourse.julialang.org/t/structure-binding-in-julia/90931/1 "2022-11-28T03:21:01Z")

</div>

Is there anyway to destructure using variable names different from fields?

```julia
struct S
    x::Int
    y::Int
end
 
s = S(1, 2)
(; x, y) = s # ok
(; a, b) = s # error: Type S has no field a 

```

In C++ I can do the following

```cpp
struct S {
    int x, y;
};

S s{1, 2};
auto [a, b] = s; 

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 28, 2022, 3:39am UTC](https://discourse.julialang.org/t/structure-binding-in-julia/90931/2 "2022-11-28T03:39:38Z")

</div>

To do this you would have to do `a,b = s.a, s.b`

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 28, 2022, 6:57am UTC](https://discourse.julialang.org/t/structure-binding-in-julia/90931/3 "2022-11-28T06:57:18Z")

</div>

You can define iteration for your type (if that makes sense):

```julia
struct S
    x::Int
    y::Int
end

function Base.iterate(s::S, i = 1)
    if i <= nfields(s)
        return (getfield(s, i), i + 1)
    else
        return nothing
    end
end

```

```julia
julia> for s in S(3,4)
           @show s
       end
s = 3
s = 4

julia> a, b = S(6, 1);

julia> a
6

julia> b
1

```

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [November 28, 2022, 8:27am UTC](https://discourse.julialang.org/t/structure-binding-in-julia/90931/4 "2022-11-28T08:27:08Z")

</div>

If you use your structure as a simple “data container” (not using it in dispatching etc.), then maybe a named tuple would fit you?

```julia
julia> s = (x=1, y=2)
(x = 1, y = 2)

julia> s[:x]
1

julia> s.x
1

julia> a, b = s
(x = 1, y = 2)

```

or just a tuple

```julia
julia> s = (1, 2)
(1, 2)

julia> s[1]
1

julia> a, b = s
(1, 2)

```

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [November 28, 2022, 8:41am UTC](https://discourse.julialang.org/t/structure-binding-in-julia/90931/5 "2022-11-28T08:41:05Z")

</div>

Edit: Ignore my answer, because I missed the point of the question …

```julia
using UnPack

@unpack a, b = s

```
