# Creating an iterative structure

**URL:** https://discourse.julialang.org/t/creating-an-iterative-structure/67088
**Category:** General Usage
**Tags:** question
**Created:** [August 27, 2021, 9:13am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088 "2021-08-27T09:13:32Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Rajesh\_Nakka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajesh_nakka/32/28445_2.png) [@Rajesh\_Nakka](https://discourse.julialang.org/u/Rajesh_Nakka)
#### Post date: [August 27, 2021, 9:13am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/1 "2021-08-27T09:13:32Z")

</div>

Given,

```julia
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[1], Points[2], Points[3]` give me` p1, p2, p3`.

Thanks in advance 🙂

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 27, 2021, 9:25am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/2 "2021-08-27T09:25:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Rajesh\_Nakka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajesh_nakka/32/28445_2.png) [@Rajesh\_Nakka](https://discourse.julialang.org/u/Rajesh_Nakka)
#### Post date: [August 27, 2021, 9:31am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/3 "2021-08-27T09:31:28Z")

</div>

> [@Sukera](#):
>
> `Points = [p1 p2 p3]`

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.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 27, 2021, 9:41am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/4 "2021-08-27T09:41:25Z")

</div>

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:

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

```

And all the arithmetic works for Point.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 27, 2021, 9:45am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/5 "2021-08-27T09:45:33Z")

</div>

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](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration) for that type. If you want indexing, implement the [indexing interface](https://docs.julialang.org/en/v1/manual/interfaces/#Indexing) 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`.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 27, 2021, 9:49am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/6 "2021-08-27T09:49:17Z")

</div>

> [@Sukera](#):
>
> `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` .

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

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 27, 2021, 9:51am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/7 "2021-08-27T09:51:15Z")

</div>

They’re not asking about a single point though, they’re asking about a collection of points:

> [@Rajesh\_Nakka](#):
>
> I want to define another iterative structure ` Points` similar to Julia Array type which can store `Point` type objects `p1, p2, p3` .

---

<div class="post-metadata">

### Author: ![Rajesh\_Nakka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajesh_nakka/32/28445_2.png) [@Rajesh\_Nakka](https://discourse.julialang.org/u/Rajesh_Nakka)
#### Post date: [August 27, 2021, 9:52am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/8 "2021-08-27T09:52:21Z")

</div>

Thanks @Sukera @lmiq .

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 27, 2021, 9:56am UTC](https://discourse.julialang.org/t/creating-an-iterative-structure/67088/9 "2021-08-27T09:56:50Z")

</div>

Yes, I know. I was just anticipating something.
