# Automatic resizing of struct internal array when value changes?

**URL:** https://discourse.julialang.org/t/automatic-resizing-of-struct-internal-array-when-value-changes/93790
**Category:** Performance
**Created:** [January 30, 2023, 8:26pm UTC](https://discourse.julialang.org/t/automatic-resizing-of-struct-internal-array-when-value-changes/93790 "2023-01-30T20:26:38Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [January 30, 2023, 8:45pm UTC](https://discourse.julialang.org/t/automatic-resizing-of-struct-internal-array-when-value-changes/93790/5 "2023-01-30T20:45:48Z")

</div>

> [@Sukera](#):
>
> ```julia
> Base.setproperty!(ts::TheStruct, s::Symbol, v)
> if s === :N
> setfield!(ts, s, v) 
> arr = getfield(ts, :x) 
> resize!(arr, v)
> else
> setfield!(ts, s, v) 
> end
> 
> ```

I have a struct made as:

```julia
# Main Struct for Gradient Values
@with_kw struct ∇ᵢWᵢⱼStruct{T,ST}
    NL::Base.RefValue{Int}

    # Input for calculation
    xᵢⱼ ::AbstractVector{SVector{ST,T}} = zeros(SVector{ST,T},NL[])
    xᵢⱼ² ::AbstractVector{T} = similar(xᵢⱼ,T,NL[])
    dᵢⱼ ::AbstractVector{T} = similar(xᵢⱼ,T,NL[])
    qᵢⱼ ::AbstractVector{T} = similar(xᵢⱼ,T,NL[])
    ∇ᵢWᵢⱼ ::AbstractVector{SVector{ST,T}} = similar(xᵢⱼ,NL[])
end

```

Where NL is the variable determining the length of each array. When I update NL, I wish for all other arrays to update in size as well.

To me this would be neat if it was done through updating “NL” automatically 🙂

The reason I use BaseValue.Ref etc. is explained more here:

> [@How to properly pass structs into GPU? (MWE included)](https://discourse.julialang.org/t/how-to-properly-pass-structs-into-gpu-mwe-included/93727/7):
>
> Thank God, I was finally able to do it exactly how I wanted! And this answers my 5th question: 5. Is the way shown here the intended and “state of the art” approach for using structs in GPU programming - if not, what is? I looked at the interpolate example in the docs, and I struggle with grasping it. Short answer, no. The code below is in my opinion much better: # Packages using CUDA using Parameters using StaticArrays using Adapt using Setfield using LinearAlgebra using BenchmarkTools # Co…

So I was hoping that by updating NL I could trigger a “reaction” updating everything which is an AbstractArray in the struct to the new size

Kind regards

---

_[View the full topic](https://discourse.julialang.org/t/automatic-resizing-of-struct-internal-array-when-value-changes/93790)._
