# Implement iteration of Struct

**URL:** <https://discourse.julialang.org/t/implement-iteration-of-struct/44459>\
**Category:** General Usage\
**Created:** [August 6, 2020, 9:47pm UTC](https://discourse.julialang.org/t/implement-iteration-of-struct/44459 "2020-08-06T21:47:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojua8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cojua8/32/11882_2.png) [@cojua8](https://discourse.julialang.org/u/cojua8)\
**Post date:** [August 6, 2020, 9:47pm UTC](https://discourse.julialang.org/t/implement-iteration-of-struct/44459/1 "2020-08-06T21:47:30Z")

</div>

Hi,

I’m trying to implement iteration on a small struct:

```julia
struct Coordinate
    x::Float64
    y::Float64
    Coordinate(x, y) = new(x, y)
end

```

So that I can unpack its members like this:

```julia
x, y = Coordinate(1,1)

```

Any help is welcome 🙂

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [August 6, 2020, 9:54pm UTC](https://discourse.julialang.org/t/implement-iteration-of-struct/44459/2 "2020-08-06T21:54:33Z")

</div>

I think all you need to do is use

[https://github.com/mauro3/UnPack.jl](https://github.com/mauro3/UnPack.jl)

---

<div class="post-metadata">

**Author:** ![cojua8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cojua8/32/11882_2.png) [@cojua8](https://discourse.julialang.org/u/cojua8)\
**Post date:** [August 6, 2020, 10:01pm UTC](https://discourse.julialang.org/t/implement-iteration-of-struct/44459/3 "2020-08-06T22:01:33Z")

</div>

Nice!,

But just as an excercise, it would be great to know how to do it “by hand”.

Also, the package you mention does not allow me to do

```julia
xc, yc = Coordinate(1,1)

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [August 6, 2020, 10:12pm UTC](https://discourse.julialang.org/t/implement-iteration-of-struct/44459/4 "2020-08-06T22:12:47Z")

</div>

All you need to do is fulfill the [iteration interface](https://docs.julialang.org/en/v1/manual/interfaces/) by defining a `Base.iterate` method, like this one:

```julia
function Base.iterate(c::Coordinate, state = 0)
    state >= nfields(c) && return
    return Base.getfield(c, state+1), state+1
end

```

(Note this one actually works for any number of fields!).

You can also define other methods mentioned in the interface like

```julia
Base.length(c::Coordinate) = nfields(c)

```

which allows e.g. `collect` to work with it.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 7, 2020, 12:16pm UTC](https://discourse.julialang.org/t/implement-iteration-of-struct/44459/5 "2020-08-07T12:16:01Z")

</div>

Just to complement this, see [`StaticArrays.FieldVector`](https://juliaarrays.github.io/StaticArrays.jl/latest/pages/api/#StaticArrays.FieldVector) which is exactly for use cases like this.
