# Array/Struct hybrids

**URL:** <https://discourse.julialang.org/t/array-struct-hybrids/9195>\
**Category:** General Usage\
**Created:** [February 20, 2018, 7:53am UTC](https://discourse.julialang.org/t/array-struct-hybrids/9195 "2018-02-20T07:53:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 20, 2018, 7:53am UTC](https://discourse.julialang.org/t/array-struct-hybrids/9195/1 "2018-02-20T07:53:39Z")

</div>

A package I’m building needs a datastructure that has properties of both structs and arrays. I’m not totally across all of the options and their relative speed or type stability. I’m currently using axis arrays and various mutable structs, but neither feels quite right. I may not quite grasp where the boundaries are on what is technically possible, but this is the wish list:

- Fields have get and set methods with fast integer indexing at run time, like arrays.
- The same fields also can be accessed with symbols, mostly at compile time, in 1 and 2 dimension - with little or no overhead.
- The field names needs to be part of the type - to alow method specialisation based on fields, like structs. I think static arrays can do this based on array length, but also using the symbols is the key.
- The largest array size that I need is around 10 \* 10.
- And of course it needs to run really fast.

Does this exist?

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [February 20, 2018, 8:37am UTC](https://discourse.julialang.org/t/array-struct-hybrids/9195/2 "2018-02-20T08:37:05Z")

</div>

Have you looked at [http://juliaarrays.github.io/StaticArrays.jl/latest/pages/api.html#StaticArrays.FieldVector](http://juliaarrays.github.io/StaticArrays.jl/latest/pages/api.html#StaticArrays.FieldVector) ? From your question I am not sure if the arrays in “both structs and arrays” are actually vectors, but if that is the case, then you might want to have a look.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 21, 2018, 2:20am UTC](https://discourse.julialang.org/t/array-struct-hybrids/9195/3 "2018-02-21T02:20:21Z")

</div>

Thanks! Field vector seems to be _exactly_ what I’m looking for.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 21, 2018, 2:23am UTC](https://discourse.julialang.org/t/array-struct-hybrids/9195/4 "2018-02-21T02:23:58Z")

</div>

Although of course not for the 2 dimensional arrays, I almost forgot about that requirement in my excitement at finding them. But they definitely tick off the 1 dimensional struct/arrays.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 21, 2018, 3:56am UTC](https://discourse.julialang.org/t/array-struct-hybrids/9195/5 "2018-02-21T03:56:33Z")

</div>

> [@Raf](#):
>
> The same fields also can be accessed with symbols, mostly at compile time, in 1 and 2 dimension - with little or no overhead.

How do you want to do this in 2D, `arr[:a,:b]` or `arr.a.b`? Does a field vector of field vectors come close to what you want? You can use a field vector of field vectors and define a symbolic `getindex` for your type that transforms `arr[:a,:b]` to `arr.a.b`.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 21, 2018, 11:44pm UTC](https://discourse.julialang.org/t/array-struct-hybrids/9195/6 "2018-02-21T23:44:16Z")

</div>

A custom getindex and setindex over field vectors should really work - I wont even have to change any code, and it will look more like what people expect of a matrix. Thanks!
