I have a manually defined type, or called struct in Julia,
mutable struct Mean_covar
mu::Array{Float64,2}
sigma::Array{Float64,2}
w::Float64
end
Now I want to define an const array but its content can change (I mean, just lock the type of the array), so I know I can use Ref.
Now the question is, how to define such an array? Let us just call it B, and say, the length of B array is 10.
I was thinking doing things like,
global const B = Ref{Array{Mean_covar,1}(undef,10)}
But obviously it gives me an error.
So,
how do I define such an array B, whose length is 10, and each element is type Mean_covar?
If I were able to define such an array B, how to set the value to its element? Like, If I want to set values to its 1st value B[1], can I do
B[1] = Mean_covar(zeros(2,2),zeros(2,2),0.0)
However the above command does not seem to work for something with a Ref property. With Ref property, it seems we need a bra [ and ket ] somewhere?
You almost never have to use Ref in julia. It’s like a single element array but most importantly arrays and Refs in julia are not interchangeable (unlike in C, where an array is just fancy syntax for pointeroffset + deref).
Assuming B is an array, this should always work. What you’ve probably been doing is a Ref of/to an array (i.e. Ref{Array{Mean_covar, 1}}, which you’d have to index like B[][1] to first get the array out of the Ref and then index into the array).
If you just have a Array{Mean_covar, 1}, B[1] is sufficient for indexing into it.
You’re probably going to be interested in the following section of the docs:
Furthermore, unless you plan to mutate the w inside your struct, you don’t need Mean_covar to be mutable. Even if Mean_covar is immutable, you can still mutate mu and sigma.