# MutatePlainDataArray.jl: Mutate nested immutables in arrays safely and efficiently

**URL:** <https://discourse.julialang.org/t/mutateplaindataarray-jl-mutate-nested-immutables-in-arrays-safely-and-efficiently/81328>\
**Category:** Package Announcements\
**Tags:** announcement, struct\
**Created:** [May 19, 2022, 7:39pm UTC](https://discourse.julialang.org/t/mutateplaindataarray-jl-mutate-nested-immutables-in-arrays-safely-and-efficiently/81328 "2022-05-19T19:39:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![drelatgithub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drelatgithub/32/36402_2.png) [@drelatgithub](https://discourse.julialang.org/u/drelatgithub)\
**Post date:** [May 19, 2022, 7:39pm UTC](https://discourse.julialang.org/t/mutateplaindataarray-jl-mutate-nested-immutables-in-arrays-safely-and-efficiently/81328/1 "2022-05-19T19:39:45Z")

</div>

[MutatePlainDataArray.jl](https://github.com/medyan-dev/MutatePlainDataArray.jl) is a tiny package that specifically aims to make mutating vectors of nested immutable fields easy.

The package exports only one function `aref`, that wraps a vector allowing subsequent mutation of nested fields using the following syntax:

```julia
    aref(v)[i].a.b._i._j[] = val

```

where `_i` and `_j` indicates getting fields in an immutable structs using index `i` and `j`, and can be used to access, for example, elements of a `Tuple`.

The mutation utilizes pointer arithmetics such that the final mutation is simply one memory assignment. The type constraints are enforced at type inference time, and bound checks are enabled unless `@inbounds` is used.

We also have an experimental feature that allows omission of the final `[]`, but I would also like to know your thoughts!

Background for making this package: In some of our internal projects, we organize some data using nested (immutable) structs and group multiple of them in an array, but it becomes a hassle to change any one nested field in the entire array due to its immutability. We used [Setfield.jl](https://github.com/jw3126/Setfield.jl) but the emitted code is not optimized enough. Therefore, inspired by [UnsafePointers.jl](https://github.com/cjdoris/UnsafePointers.jl), we made this package that makes such mutations much easier and more efficient.
