# Can fields of structure be indexed by number?

**URL:** <https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 31, 2021, 12:37pm UTC](https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423 "2021-08-31T12:37:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [August 31, 2021, 12:37pm UTC](https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423/1 "2021-08-31T12:37:14Z")

</div>

It is know that accessing a field of structure is by using the dot method. Such as, accessing the first field of the below structure is Point.x. However, I would like to access the fields by using a index number accompanied to the name of structure (i.e. as in matrix).  
For example instead of using Point.x, I need to use something like Point(1) or Point[1]. Any way to do so?

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

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 31, 2021, 12:39pm UTC](https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423/2 "2021-08-31T12:39:56Z")

</div>

I guess you could define a `getindex` method for your struct like this:

```julia
julia> struct Point
        x::Int
        y::Int
       end

julia> Base.getindex(x::Point, i) = getproperty(x, fieldnames(typeof(x))[i])

julia> a = Point(10, 20)
Point(10, 20)

julia> a[1]
10

julia> a[2]
20

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 31, 2021, 12:51pm UTC](https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423/3 "2021-08-31T12:51:01Z")

</div>

> [@nilshg](#):
>
> `fieldnames(typeof(x))`

I low key suspect this will be slow

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 31, 2021, 1:01pm UTC](https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423/4 "2021-08-31T13:01:42Z")

</div>

Not sure how to benchmark this properly, seems everything is figured out by the compiler:

```julia
julia> @btime $a[2]
  0.001 ns (0 allocations: 0 bytes)
20

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 31, 2021, 1:06pm UTC](https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423/5 "2021-08-31T13:06:35Z")

</div>

we do have aggressive constant prop for sure…

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 31, 2021, 1:12pm UTC](https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423/6 "2021-08-31T13:12:28Z")

</div>

It looks to me like you should subtype `FieldVector` from the StaticArrays package: [API · StaticArrays.jl](https://juliaarrays.github.io/StaticArrays.jl/latest/pages/api/#FieldVector)

This looks like a classic application of this. You will get lots of functionality for free, and excellent performance.

And if you want to implement it yourself, you could study its source code.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [August 31, 2021, 1:47pm UTC](https://discourse.julialang.org/t/can-fields-of-structure-be-indexed-by-number/67423/7 "2021-08-31T13:47:26Z")

</div>

Wouldn’t the best be `Base.getindex(x::Point, i::Int) = getfield(x, i)`
