Implement iteration of Struct

All you need to do is fulfill the iteration interface by defining a Base.iterate method, like this one:

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

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

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

6 Likes