# Setting individual fields for struct in array

**URL:** <https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300>\
**Category:** New to Julia\
**Created:** [August 9, 2017, 12:16pm UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300 "2017-08-09T12:16:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [August 9, 2017, 12:16pm UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300/1 "2017-08-09T12:16:53Z")

</div>

Could anyone suggest how to make the following:

```julia
struct A b::Int; c::Int end
as = [A(1, 1), A(2, 2)]
as[2].c = 3

```

work _without_ resorting to:

- mutable types
- recreating `A` with a changed field

Ideally the produced `llvm` code might be similar to C equivalent `(*pointer).c = 3`, so the solution may involve `Ref`, `unsafe` methods etc. I checked the documentation, and some GitHub threads, but still can’t figure out how to do this and bypass Julia’s convention for immutability in this particular case.

---

<div class="post-metadata">

**Author:** ![jonas-kr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-kr/32/1810_2.png) [@jonas-kr](https://discourse.julialang.org/u/jonas-kr)\
**Post date:** [August 9, 2017, 1:44pm UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300/2 "2017-08-09T13:44:55Z")

</div>

I would recommend the following procedure to you:

```julia
struct A{T <: Integer} 
b::Vector{T}
c::Vector{T}
A(b::T, c::T) where T = new{T}([b], [c])
end

as = [A(1, 1); A(2, 2)]
as[2].c[1] = 3

```

Maybe it’s even possible to implement a specialized value to array conversion such that you do not have to use indexing in the last expression…

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [August 9, 2017, 2:52pm UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300/3 "2017-08-09T14:52:07Z")

</div>

See these issues that give deep insights of what’s going on about this:  
[https://github.com/JuliaLang/julia/pull/21912](https://github.com/JuliaLang/julia/pull/21912)  
[https://github.com/JuliaLang/julia/issues/17115](https://github.com/JuliaLang/julia/issues/17115)

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [August 9, 2017, 3:24pm UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300/4 "2017-08-09T15:24:27Z")

</div>

In the meantime, you could try something like this

```julia
struct A{T<:Integer}
    b::Base.RefValue{T}
    c::Base.RefValue{T}
end
A(b::T, c::T) where {T<:Integer} = A{T}(Ref(b), Ref(c))

Base.show(io::IO, a::A) = print(io, "A(", a.b[], ", ", a.c[], ")")

as = [A(1, 1); A(2, 2)]
as[2].c[] = 3

```

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [August 9, 2017, 3:48pm UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300/5 "2017-08-09T15:48:24Z")

</div>

Also, see this previous post [Mutable field in immutable type](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103) and this comment on [Immutable vs Type: reassignment via arrays and performance - #10 by jameson](https://discourse.julialang.org/t/immutable-vs-type-reassignment-via-arrays-and-performance/3364/10).

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [August 10, 2017, 8:08am UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300/6 "2017-08-10T08:08:32Z")

</div>

Thank you @cdsousa, but some code snippets which I encountered in these threads will be available in 1.0+, as I understood. At least methods mentioned are not available in 0.6. It is just very strange that there is no way to do something similar, even with some low-level code, in the current version. But I suspect that I’m missing something, if you would be able to provide a short snippet of working code, I would be grateful.

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [August 10, 2017, 8:10am UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300/7 "2017-08-10T08:10:43Z")

</div>

@PabloZubieta Is it possible to not uglify the `struct` definition, and obtain these `RefValue`s in some other way?

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [August 10, 2017, 8:16am UTC](https://discourse.julialang.org/t/setting-individual-fields-for-struct-in-array/5300/8 "2017-08-10T08:16:49Z")

</div>

@jonas-kr your solution introduces mutable objects (`Vector`), this makes the whole point of using immutable `struct`s in array irrelevant.
