# Composite types with array type fields

**URL:** https://discourse.julialang.org/t/composite-types-with-array-type-fields/114253
**Category:** New to Julia
**Tags:** question
**Created:** [May 14, 2024, 3:05pm UTC](https://discourse.julialang.org/t/composite-types-with-array-type-fields/114253 "2024-05-14T15:05:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![erenust](https://avatars.discourse-cdn.com/v4/letter/e/b2d939/32.png) [@erenust](https://discourse.julialang.org/u/erenust)
#### Post date: [May 14, 2024, 3:05pm UTC](https://discourse.julialang.org/t/composite-types-with-array-type-fields/114253/1 "2024-05-14T15:05:44Z")

</div>

I am trying to understand the strange behavior I see in the code below.

```julia
struct A
    m::Vector{Int32}
end

struct B
    m::Vector{Vector{Int32}}
end

let
    # Here, the assigment works like a pointer assigment
    a1 = A([1,2,3])
    a2 = A(a1.m)
    @show a1.m === a2.m
    a1.m[1] = 2
    @show a1, a2
    # Here it performs a copy
    arr = [1,2,3]
    a3 = A(arr)
    @show a3.m === arr
    arr[1] = 2
    @show arr, a3
    # The same thing here
    b1 = B([[1,2],[3]])
    b2 = B(b1.m)
    @show b1.m === b2.m
    b1.m[1] = [1]
    @show b1, b2
    arr = [[1,2],[3]]
    b3 = B(arr)
    @show b3.m === arr
    # and it's a deepcopy
    arr[1][1] = 2
    @show arr, b3
end

```

The output I get is

```julia
a1.m === a2.m = true
(a1, a2) = (A(Int32[2, 2, 3]), A(Int32[2, 2, 3]))
a3.m === arr = false
(arr, a3) = ([2, 2, 3], A(Int32[1, 2, 3]))
b1.m === b2.m = true
(b1, b2) = (B(Vector{Int32}[[1], [3]]), B(Vector{Int32}[[1], [3]]))
b3.m === arr = false
(arr, b3) = ([[2, 2], [3]], B(Vector{Int32}[[1, 2], [3]]))

```

I find this really strange. I was hoping to use an immutable struct as a simple container for a few pointers, but it seems that in some cases it performs a deepcopy of the input array. What is the difference between these two cases, how do I know when it’s only the pointer being copied vs. the resource handled by the pointer being copied? (I am assuming that the behavior here extends to mutable type fields in general.)

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 14, 2024, 3:09pm UTC](https://discourse.julialang.org/t/composite-types-with-array-type-fields/114253/2 "2024-05-14T15:09:46Z")

</div>

```julia
arr = [1,2,3]
a3 = A(arr)

```

The type of `arr` is not the same as the `m` field, so there must be a conversion, and the result of the conversion is clearly not `===` the original. If you don’t want the conversion, use `arr = Int32[1,2,3]`.

In the first case, you are only comparing the fields, post-conversion.

---

<div class="post-metadata">

### Author: ![erenust](https://avatars.discourse-cdn.com/v4/letter/e/b2d939/32.png) [@erenust](https://discourse.julialang.org/u/erenust)
#### Post date: [May 14, 2024, 3:12pm UTC](https://discourse.julialang.org/t/composite-types-with-array-type-fields/114253/3 "2024-05-14T15:12:46Z")

</div>

That was quick, thanks a lot! I had forgotten about type conversion.
