# How to define an array whose elements are manually defined type?

**URL:** <https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776>\
**Category:** General Usage\
**Created:** [August 4, 2021, 4:17am UTC](https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776 "2021-08-04T04:17:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 4, 2021, 4:17am UTC](https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776/1 "2021-08-04T04:17:24Z")

</div>

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,

1. how do I define such an array B, whose length is 10, and each element is type Mean\_covar?

2. 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

Thanks!

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 4, 2021, 4:52am UTC](https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776/2 "2021-08-04T04:52:36Z")

</div>

`const B = Array{Mean_covar,1}(undef, 10)` works well because `Array`’s are mutable.

Input:

```julia
mutable struct Mean_covar
    mu::Array{Float64,2}
    sigma::Array{Float64,2}
    w::Float64 
end

const B = Array{Mean_covar,1}(undef, 5)
display(B)

```

Output:

```julia
5-element Vector{Mean_covar}:
 #undef
 #undef
 #undef
 #undef
 #undef

```

Input:

```nohighlight
B[1] = Mean_covar(zeros(2,2), zeros(2,2), 0.0)
display(B)

```

Output:

```julia
5-element Vector{Mean_covar}:
    Mean_covar([0.0 0.0; 0.0 0.0], [0.0 0.0; 0.0 0.0], 0.0)
 #undef
 #undef
 #undef
 #undef

```

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 4, 2021, 5:03am UTC](https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776/3 "2021-08-04T05:03:05Z")

</div>

Thank you very much!  
In this case, do I need to add a Ref to B?  
like

```
const B = Ref{Array{Mean_covar,1}(undef,10)}

```

or just as you said, no need to add Ref?

When to use Ref and when not to use Ref?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 4, 2021, 5:08am UTC](https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776/4 "2021-08-04T05:08:17Z")

</div>

`const c = Ref(ImmutableObject) ` or `const c = MutableObject`.

`Ref(x)`, `Vector{T}(undef, n)`, and `Array{T, N}(undef, k_1, ..., k_N)` are mutable, so no need to add `Ref`.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 4, 2021, 5:50am UTC](https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776/5 "2021-08-04T05:50:58Z")

</div>

> [@CRquantum](#):
>
> 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.

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).

> [@CRquantum](#):
>
> Like, If I want to set values to its 1st value B[1], can I do
> 
> ```julia
> B[1] = Mean_covar(zeros(2,2),zeros(2,2),0.0)
> 
> ```

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:

[https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-C/C](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-C/C)

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 4, 2021, 6:11am UTC](https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776/6 "2021-08-04T06:11:25Z")

</div>

Thank you! Like you said, for const array there is no need to add Ref, since array type is mutable. The Ref trick is most likely for a variable.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 4, 2021, 9:19am UTC](https://discourse.julialang.org/t/how-to-define-an-array-whose-elements-are-manually-defined-type/65776/7 "2021-08-04T09:19:50Z")

</div>

> [@CRquantum](#):
>
> ```julia
> mutable struct Mean_covar
> mu::Array{Float64,2}
> sigma::Array{Float64,2}
> w::Float64 
> end
> 
> ```

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`.
