# How to make StructArray work with Static LabelledArray?

**URL:** https://discourse.julialang.org/t/how-to-make-structarray-work-with-static-labelledarray/114132
**Category:** New to Julia
**Created:** [May 11, 2024, 5:37am UTC](https://discourse.julialang.org/t/how-to-make-structarray-work-with-static-labelledarray/114132 "2024-05-11T05:37:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![freedomjin](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@freedomjin](https://discourse.julialang.org/u/freedomjin)
#### Post date: [May 11, 2024, 5:37am UTC](https://discourse.julialang.org/t/how-to-make-structarray-work-with-static-labelledarray/114132/1 "2024-05-11T05:37:21Z")

</div>

The following code does not work as expected:

```julia
>using StructArrays, LabelledArrays
>SLV2 = @SLVector Int (:x, :y)
>vf=StructArray([rand(SLV2) for _ in 1:5])
>vf.x
ERROR: type Tuple has no field x

```

The same code with `SLVector` replaced by `<:FieldVector` actually works.  
Is there a way to make this work with `LabelledArrays`?  
Thanks in advance.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [May 11, 2024, 6:12am UTC](https://discourse.julialang.org/t/how-to-make-structarray-work-with-static-labelledarray/114132/2 "2024-05-11T06:12:16Z")

</div>

What is the goal you are trying to achieve? Can you maybe provide the code that works and what it works for?

Your current `vf` is a tuple, so sure it does not have a `vf.x`, but every single entry of said tuple is an `SLArray` and hence has an `.x`, for example ` vf[1].x` does work.

Do you maybe want to have a vector of all these `x`es? That would be something like

```julia
[e.x for e in vf]

```

---

<div class="post-metadata">

### Author: ![freedomjin](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@freedomjin](https://discourse.julialang.org/u/freedomjin)
#### Post date: [May 11, 2024, 7:37am UTC](https://discourse.julialang.org/t/how-to-make-structarray-work-with-static-labelledarray/114132/3 "2024-05-11T07:37:51Z")

</div>

> [@kellertuer](#):
>
> Your current `vf` is a tuple

Here the goal is to use `StructArray` to represent Array of field vectors as vector of arrays. `vf` is supposed to be such a “vector field” where each component is stored as a whole array, and can be accessed by `vf.x`, etc., and `vf[1]` can be used to access the 1st field vector, e.g., the vector formed by 1st elements of `vf.x` and `vf.y`, resp.. All these work for `FieldVector`, but not for `SLVector`, i.e., labelled vector with names for each component.

The reason to want to use `SLVector` instead of `FieldVector` is that the latter does not offer an easy constructor as the former, and one must define a type explicitly as a subtype `<:FieldVector{N,T}`. This is difficult to do if one does not know a-priori what the names of the vector components are, (e.g., :x, :y, …).

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [May 11, 2024, 8:12am UTC](https://discourse.julialang.org/t/how-to-make-structarray-work-with-static-labelledarray/114132/4 "2024-05-11T08:12:23Z")

</div>

Hm, then the data structure you chose (`StructArray`) is just not want you describe you want, It stores a vector of the vectors, and not each component separately as a vector internally (that you could access with `.x`).  
So `[e.x for e in vf]` extracts this vector of first components (but allocates), simply because the data structure is completely different.

So if you want an easy constructor, you loose the `vf.x` easy access. The code above does give you the same result, _but_ it allocates.

Maybe a comparison here is, you exchanged your bike with a car, but that is at the cost of the car making more noise, but is more comfortable/faster. You can not have both.

PS: Just saw at the top, this is your first post – welcome to the Julia forum 👋
