# Creating an iterative structure

Given,

``````struct Point
x::Float64
y::Float64
z::Float64
end
p1 = Point(1.0, 0.0, 0.0)
p2 = Point(1.0, 1.0, 0.0)
p3 = Point(1.0, 0.0, 1.0)
``````

I want to define another iterative structure` Points` similar to Julia Array type which can store `Point` type objects `p1, p2, p3`. I am thinking, how to construct such an array so that `Points, Points, Points` give me` p1, p2, p3`.

Thanks in advance Is `Points = [p1 p2 p3]` not sufficient for your usecase? What do you want to do with `Points`?

This gives `Points` of `Vector` type. I am asking, if there is any way to store objects similar to this `[p1 p2 p3]` but have a new user defined type.

You can define a type that is a subtype of arrays, but the question is what you want that structure to do that a vector of points does not?

By the way, take a look at StaticArrays, and the FieldVector super type, for defining your Points.

With FieldVector from StaticArrays you get define:

``````struct Point{T} <: FieldVector{3,T}
x::T
y::T
z::T
end
``````

And all the arithmetic works for Point.

1 Like

Well, what do you want to do with your hypothetical new `Points` type? Do you want to store a fixed number of points? An arbitrary number? What would a `Points` type to differently to a regular `Vector`?

If you just want to iterate over something and dispatch differently to `Vector`, you can define a simple wrapper type and implement the iterator interface for that type. If you want indexing, implement the indexing interface for that type (you’ll probably have to forward a bunch of methods to a wrapped `Vector` or `Tuple`).

`FieldVector` from `StaticArrays` is advanced usage - I’d encourage you to first familiarize yourself with how indexing and iteration works in julia and finding out how you want to use your new type before jumping into performance optimizations like `StaticArrays`.

2 Likes

For this particular use it actually helps a lot basic usage, because without it the user has to define it’s own basic arithmetic operators for summing, averaging, etc, the custom Points