Understaing vector (or array?) fields in julia mutable struct

Hello everyone! I’m a mechanical engineer coming from MATLAB and relatively new to Julia, so I’m really sorry for my heresies!

I’m currently working on mutable structures, and I’m interested in creating an array (or vector?) of structures like the following:

    mutable struct s3_str
        c::Int64
        d::Float64
        s3() = new(0,0.)
    end
  mutable struct s2_str
      a::Int64
      b::Array{Int64,1}
      s3 #I would like to have a vector field of s3 structures here
      s2() = new(0, [0], s3()) # is it right?
  end
mutable struct s1
    s2 #I would like to have a vector field of s2 structures here
    s1() = new(s2()) # is it right?
end

that I would like to call like:

s1[i].b = some array of integers
s1[i].s2[j].c = some integer

Could you please tell me:

  1. how to define the fields of the structs
  2. how to preallocate the array of structures s1[ - ] of length n, containing the array of structures s2[ - ] of length m with zero-ed (or undefined) values.

Thank you all in advance, and I hope you are all healthy!

I tried to make minimal changes to get the script working:

mutable struct S3_str
    c::Int64
    d::Float64
    S3_str() = new(0,0.)
end
mutable struct S2_str
  a::Int64
  b::Array{Int64,1}
  s3::Vector{S3_str} #I would like to have a vector field of s3 structures here
  S2_str() = new(0, [0], S3_str[]) 
end
mutable struct S1
s2::Vector{S2_str} #I would like to have a vector field of s2 structures here
S1() = new(S2_str[]) 
end

s1 = S1()
push!(s1.s2, S2_str())
s1.s2[1].b = Int[1, 2, 3]
push!(s1.s2[1].s3, S3_str())
s1.s2[1].s3[1].c = 4
@show s1

yielding

s1 = S1(S2_str[S2_str(0, [1, 2, 3], S3_str[S3_str(4, 0.0)])])
2 Likes

Thanks a lot!