Mutable struct with multi dimensional Array as a field


#1

The code below is my attempt at creating a mutable struct that has an Array as a field.

Does this seem to be the correct method of creating the Array?
It seems a little unnatural of how the length and dims are created after the fact!

#-- a mutable struct containing two vars and an array
mutable struct MyData
  Hits::Int  #-- var1 of type Int
  Skips::Int #-- var2 of type Int
  SomeArray::Array{Int}  #-- Array type of Int => Note: no dims or length seem to be allowed
end

#--- intialize VarData in following two lines
VarData = MyData(0, 0, repeat(0:0, outer = 12))  #--- initialize vars to 0, and Array to length 12x1 with val 0
VarData.SomeArray = reshape(VarData.SomeArray, 4,3)  #-- reshape Array into desired dims of 4x3
#--- is there a (better) way to combine the preceeding two lines into one line?

#2

SomeArray::Array{Int,2} etc. to be explicit, or SomeArray::Array{Int,N} with mutable struct MyData{N}

Or are you looking for VarData = MyData(0, 0, zeros(Int,4,3))?


#3

I was looking for your zeros alternative. That works!

I thought I had tried it but must have typed it wrong. Thanks again…Arch


#4

This SO question is similar and may also be helpful:

https://stackoverflow.com/questions/45069914/defined-type-with-unspecified-dimension-array-in-julia