# \#mutable-structure

**URL:** https://discourse.julialang.org/tag/mutable-structure/501.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [\[ANN\]: ZeroDimensionalArrays.jl: zero-dimensional arrays/references/boxes](https://discourse.julialang.org/t/ann-zerodimensionalarrays-jl-zero-dimensional-arrays-references-boxes/128002)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 24\
**Last updated:** [October 15, 2025, 3:33pm UTC](https://discourse.julialang.org/t/ann-zerodimensionalarrays-jl-zero-dimensional-arrays-references-boxes/128002 "2025-10-15T15:33:59Z")

</div>

ZeroDimensionalArrays.jl is being registered, meaning it has entered the three-day-long wait period. Feel free to suggest breaking changes until the wait period is over! It’s a tiny package, implementing several similar…

---

## [Struct with read-only and write-only properties](https://discourse.julialang.org/t/struct-with-read-only-and-write-only-properties/129179)

<div class="topic-metadata">

**Author:** [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Replies:** 7\
**Last updated:** [May 21, 2025, 7:15pm UTC](https://discourse.julialang.org/t/struct-with-read-only-and-write-only-properties/129179 "2025-05-21T19:15:16Z")

</div>

Suppose I have the following mutable struct @kwdef mutable struct Rectangle{T} x::T = 0 y::T = 0 function Rectangle(args...) return new{promote\_type(typeof.(args)...)}(args...) end end I would like for this…

---

## [Mutable structs seem cause way more allocations in multithreaded code](https://discourse.julialang.org/t/mutable-structs-seem-cause-way-more-allocations-in-multithreaded-code/128156)

<div class="topic-metadata">

**Author:** [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Replies:** 2\
**Last updated:** [April 17, 2025, 10:10am UTC](https://discourse.julialang.org/t/mutable-structs-seem-cause-way-more-allocations-in-multithreaded-code/128156 "2025-04-17T10:10:34Z")

</div>

Hi everyone, I have encountered a pretty annoying performance bottleneck when using StatsBase.Weights, which is a mutable struct (whatever may be the reason for that :P) , in a multithreaded setting. After going down t…

---

## [Julep: Efficient Hierarchical Mutable Data](https://discourse.julialang.org/t/julep-efficient-hierarchical-mutable-data/125190)

<div class="topic-metadata">

**Author:** [@PatrickHaecker](https://discourse.julialang.org/u/PatrickHaecker)\
**Replies:** 47\
**Last updated:** [February 12, 2025, 4:54pm UTC](https://discourse.julialang.org/t/julep-efficient-hierarchical-mutable-data/125190 "2025-02-12T16:54:12Z")

</div>

TL;DR This proposal outlines a method to achieve space and time-efficient writable substructures in Julia. The memory layout is identical to that of immutable structs within mutable structs. This approach allows to expre…

---

## [Mutable struct vs Ref in an immutable one?](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 7\
**Last updated:** [November 28, 2024, 6:04pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846 "2024-11-28T18:04:24Z")

</div>

I want to define a struct where one field is mutable, and one isn’t,. I see two ways to approach this: struct A x :: Int y :: typeof(Ref(1)) end and mutable struct B const x :: Int y :: Int end Wh…

---

## [C struct garbage collection not run frequently enough](https://discourse.julialang.org/t/c-struct-garbage-collection-not-run-frequently-enough/116919)

<div class="topic-metadata">

**Author:** [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Replies:** 28\
**Last updated:** [July 14, 2024, 11:42pm UTC](https://discourse.julialang.org/t/c-struct-garbage-collection-not-run-frequently-enough/116919 "2024-07-14T23:42:04Z")

</div>

I have a Julia interface to a C library where each C struct represents a number. In Julia, the mutable struct just contains a Ptr to the C struct, and the finalizer calls the C destructor. These C structs can be very me…

---

## [Comparing mutable structs with identical field values in Julia](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586)

<div class="topic-metadata">

**Author:** [@baiango](https://discourse.julialang.org/u/baiango)\
**Replies:** 5\
**Last updated:** [July 3, 2024, 3:03pm UTC](https://discourse.julialang.org/t/comparing-mutable-structs-with-identical-field-values-in-julia/116586 "2024-07-03T15:03:26Z")

</div>

Description I am working on a project that involves comparing mutable structs with identical field values in Julia. However, I am encountering unexpected results when using the == and === operators, as they return false …

---

## [Help me understand the \`unique\` semantic for a vector of mutable struct](https://discourse.julialang.org/t/help-me-understand-the-unique-semantic-for-a-vector-of-mutable-struct/110887)

<div class="topic-metadata">

**Author:** [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Replies:** 6\
**Last updated:** [February 28, 2024, 12:01pm UTC](https://discourse.julialang.org/t/help-me-understand-the-unique-semantic-for-a-vector-of-mutable-struct/110887 "2024-02-28T12:01:51Z")

</div>

The unique documentation mentions that it is based on the isequal function. Hence I cannot understand the result of the following MWE: Base.@kwdef mutable struct AStruct toto::String = "toto" tuti::Int …

---

## [Best practices for organizing objects](https://discourse.julialang.org/t/best-practices-for-organizing-objects/108305)

<div class="topic-metadata">

**Author:** [@Gast](https://discourse.julialang.org/u/Gast)\
**Replies:** 3\
**Last updated:** [January 3, 2024, 4:45pm UTC](https://discourse.julialang.org/t/best-practices-for-organizing-objects/108305 "2024-01-03T16:45:47Z")

</div>

I wonder if how I call objects through different functions is the ‘best way’ of doing it (or if at least is in line with Julia’s best practices). A general example of what I am doing is below. All objects needed in my p…

---

## [Equality of mutable structs](https://discourse.julialang.org/t/equality-of-mutable-structs/107146)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 7\
**Last updated:** [December 5, 2023, 12:40pm UTC](https://discourse.julialang.org/t/equality-of-mutable-structs/107146 "2023-12-05T12:40:44Z")

</div>

should this be the case ? mutable struct X x::Int64 end X(1)==X(1) #false

---

## [Copying mutable structs without using deepcopy()](https://discourse.julialang.org/t/copying-mutable-structs-without-using-deepcopy/106648)

<div class="topic-metadata">

**Author:** [@rolim.gustavo](https://discourse.julialang.org/u/rolim.gustavo)\
**Replies:** 4\
**Last updated:** [November 24, 2023, 8:37am UTC](https://discourse.julialang.org/t/copying-mutable-structs-without-using-deepcopy/106648 "2023-11-24T08:37:30Z")

</div>

Dear colleagues, I am working on an optimization problem where I have to group a permutation of jobs, which have some characteristics. A solution to this problem can be easily represented as a vector of vectors of a typ…

---

## [Tracking memory allocation overhead in @time on mutable struct code](https://discourse.julialang.org/t/tracking-memory-allocation-overhead-in-time-on-mutable-struct-code/102519)

<div class="topic-metadata">

**Author:** [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Replies:** 0\
**Last updated:** [August 5, 2023, 11:56am UTC](https://discourse.julialang.org/t/tracking-memory-allocation-overhead-in-time-on-mutable-struct-code/102519 "2023-08-05T11:56:10Z")

</div>

I was making some experiments with the code mentioned in this StackOverflow thread In julia, why is the memory allocated the same, but the number of allocations vastly different? - Stack Overflow It relates to this blog…

---

## [Mutable vs immutable struct: modifying an array field](https://discourse.julialang.org/t/mutable-vs-immutable-struct-modifying-an-array-field/94976)

<div class="topic-metadata">

**Author:** [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Replies:** 4\
**Last updated:** [February 22, 2023, 8:15am UTC](https://discourse.julialang.org/t/mutable-vs-immutable-struct-modifying-an-array-field/94976 "2023-02-22T08:15:32Z")

</div>

I am confused about the mutable vs immutable struct documentation. For composite types, this means that the identity of the values of its fields will never change. When the fields are bits types, that means their bits …

---

## [How to broadcast a scalar multiplication through a Mesh?](https://discourse.julialang.org/t/how-to-broadcast-a-scalar-multiplication-through-a-mesh/93038)

<div class="topic-metadata">

**Author:** [@g2g](https://discourse.julialang.org/u/g2g)\
**Replies:** 2\
**Last updated:** [January 17, 2023, 1:07am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-scalar-multiplication-through-a-mesh/93038 "2023-01-17T01:07:18Z")

</div>

I’m trying to port a JavaScript 3D slicer example to Julia. The example loads a 3D model of a bunny (from https://enkimute.github.io/ganja.js/examples/bunny.obj having 5002 triangle faces) and then calculates horizontal …

---

## [Copyto! issue when updating fields of struct](https://discourse.julialang.org/t/copyto-issue-when-updating-fields-of-struct/85052)

<div class="topic-metadata">

**Author:** [@WDM-Computation](https://discourse.julialang.org/u/WDM-Computation)\
**Replies:** 8\
**Last updated:** [July 31, 2022, 6:26pm UTC](https://discourse.julialang.org/t/copyto-issue-when-updating-fields-of-struct/85052 "2022-07-31T18:26:25Z")

</div>

Hello! This is somewhat of a followup question from before. I have some structs using StaticArrays for performance reasons, which has been working great until now. It seems that as long as I am broadcasting ( ‘.=’ ) an …

---

## [Small changes on large arrays of custom types: mutable or immutable? shallow or deepcopy?](https://discourse.julialang.org/t/small-changes-on-large-arrays-of-custom-types-mutable-or-immutable-shallow-or-deepcopy/82335)

<div class="topic-metadata">

**Author:** [@skraemer](https://discourse.julialang.org/u/skraemer)\
**Replies:** 3\
**Last updated:** [June 7, 2022, 12:56pm UTC](https://discourse.julialang.org/t/small-changes-on-large-arrays-of-custom-types-mutable-or-immutable-shallow-or-deepcopy/82335 "2022-06-07T12:56:51Z")

</div>

(I have previously asked this on the New to Julia category, but I think might rather belong here) There are a few things somehow all related about the following situation concerning mutability to which I can not find a …

---

## [Requesting code review for a mutable struct and use of pointers. (53 lines of code)](https://discourse.julialang.org/t/requesting-code-review-for-a-mutable-struct-and-use-of-pointers-53-lines-of-code/80954)

<div class="topic-metadata">

**Author:** [@ao222](https://discourse.julialang.org/u/ao222)\
**Replies:** 2\
**Last updated:** [May 12, 2022, 6:34pm UTC](https://discourse.julialang.org/t/requesting-code-review-for-a-mutable-struct-and-use-of-pointers-53-lines-of-code/80954 "2022-05-12T18:34:46Z")

</div>

This is an implementation of a floyd cycle detection algorithm on a linked list. My goal is just to learn from the exercise. Is my mutable struct and constructor with two helper functions an acceptable way to approach t…

---

## [Parameter structures and restrictions](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892)

<div class="topic-metadata">

**Author:** [@Joao\_Barata](https://discourse.julialang.org/u/Joao_Barata)\
**Replies:** 13\
**Last updated:** [May 12, 2022, 3:48pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892 "2022-05-12T15:48:21Z")

</div>

Hi, I’m using a parameter structure for use in an economic model in Julia. I’d like to know what are the best practices here, including the best way to include restrictions when generating and modifying a parameter stru…

---

## [Understaing vector (or array?) fields in julia mutable struct](https://discourse.julialang.org/t/understaing-vector-or-array-fields-in-julia-mutable-struct/79479)

<div class="topic-metadata">

**Author:** [@francesco123212](https://discourse.julialang.org/u/francesco123212)\
**Replies:** 2\
**Last updated:** [April 14, 2022, 12:20pm UTC](https://discourse.julialang.org/t/understaing-vector-or-array-fields-in-julia-mutable-struct/79479 "2022-04-14T12:20:56Z")

</div>

Hello everyone! I’m a mechanical engineer coming from MATLAB and relatively new to Julia, so I’m really sorry for my heresies! I’m currently working on mutable structures, and I’m interested in creating an array (or vec…

---

## [Best way to store a sparse, unbalanced, and not complete binary tree](https://discourse.julialang.org/t/best-way-to-store-a-sparse-unbalanced-and-not-complete-binary-tree/74902)

<div class="topic-metadata">

**Author:** [@AlessioLapolla](https://discourse.julialang.org/u/AlessioLapolla)\
**Replies:** 18\
**Last updated:** [January 23, 2022, 12:09pm UTC](https://discourse.julialang.org/t/best-way-to-store-a-sparse-unbalanced-and-not-complete-binary-tree/74902 "2022-01-23T12:09:43Z")

</div>

I am implementing a binary tree where given the key of the root and a function the left and right children are defined up to a given depth that is an other parameter of the constructing function: create\_tree!(tree, key, …

---

## [Problem with mutation and copying of Matrices/Vectors](https://discourse.julialang.org/t/problem-with-mutation-and-copying-of-matrices-vectors/73221)

<div class="topic-metadata">

**Author:** [@Amro](https://discourse.julialang.org/u/Amro)\
**Replies:** 22\
**Last updated:** [December 21, 2021, 12:23am UTC](https://discourse.julialang.org/t/problem-with-mutation-and-copying-of-matrices-vectors/73221 "2021-12-21T00:23:34Z")

</div>

I have the below MWE of my code. Here, R.I\_prev = R.I; is working as copy but I found that it works as mutation in my code, so any change of R.I will change directly R.I\_prev. I faced this issue many times. Can any body…

---

## [Zygote.jl-compatible dictionary-like mutable data types](https://discourse.julialang.org/t/zygote-jl-compatible-dictionary-like-mutable-data-types/73186)

<div class="topic-metadata">

**Author:** [@iHany](https://discourse.julialang.org/u/iHany)\
**Replies:** 1\
**Last updated:** [December 16, 2021, 4:11pm UTC](https://discourse.julialang.org/t/zygote-jl-compatible-dictionary-like-mutable-data-types/73186 "2021-12-16T16:11:55Z")

</div>

Hi, to stack data without specified length, I utilised dictionary for my tasks. However, I found that there are some issues using dictionary with Zygote for automatic differentiation (AD), e.g., like this. Is there an…

---

## [CuArray only supports element types that are stored inline?](https://discourse.julialang.org/t/cuarray-only-supports-element-types-that-are-stored-inline/71339)

<div class="topic-metadata">

**Author:** [@Ludovic\_Dumoulin](https://discourse.julialang.org/u/Ludovic_Dumoulin)\
**Replies:** 1\
**Last updated:** [November 11, 2021, 5:23pm UTC](https://discourse.julialang.org/t/cuarray-only-supports-element-types-that-are-stored-inline/71339 "2021-11-11T17:23:19Z")

</div>

Hello, I would like to define, a mutable structure with a field of type ::CuArray{an\_other\_mutable\_structure} but I get this error: CuArray only supports element types that are stored inline I don’t understand what i…

---

## [Strange nan errors when mutating immutable struct arrays](https://discourse.julialang.org/t/strange-nan-errors-when-mutating-immutable-struct-arrays/69084)

<div class="topic-metadata">

**Author:** [@PatrickMcFarlane](https://discourse.julialang.org/u/PatrickMcFarlane)\
**Replies:** 4\
**Last updated:** [October 2, 2021, 11:46pm UTC](https://discourse.julialang.org/t/strange-nan-errors-when-mutating-immutable-struct-arrays/69084 "2021-10-02T23:46:39Z")

</div>

I’m working on a large economic model that involves a dynamic optimization problem. I’ve set up arrays within immutable structs to hold the solutions to the problem. The algorithm involves iterating on fixed points on …

---

## [Replace date year](https://discourse.julialang.org/t/replace-date-year/68301)

<div class="topic-metadata">

**Author:** [@jzr](https://discourse.julialang.org/u/jzr)\
**Replies:** 22\
**Last updated:** [September 19, 2021, 9:21pm UTC](https://discourse.julialang.org/t/replace-date-year/68301 "2021-09-19T21:21:13Z")

</div>

d = Date(2010,01,01) How can I replace the year with 2011?

---

## [How to define an element inside a structure with type 3x3 Matrix{Float64}](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357)

<div class="topic-metadata">

**Author:** [@Amro](https://discourse.julialang.org/u/Amro)\
**Replies:** 6\
**Last updated:** [September 17, 2021, 11:45pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357 "2021-09-17T23:45:20Z")

</div>

I need to define Ls to be of 3x3 Matrix{Float64}. I could write as below but it is not working, any idea to define this? mutable struct R Ls::Array{Float64, 3x3} = \[0,0,0; 0,0,0; 0,0,0\] Ls::Vector{Vector{Float6…

---

## [Hash function for immutable struct containing mutable vectors](https://discourse.julialang.org/t/hash-function-for-immutable-struct-containing-mutable-vectors/68326)

<div class="topic-metadata">

**Author:** [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Replies:** 2\
**Last updated:** [September 17, 2021, 6:24pm UTC](https://discourse.julialang.org/t/hash-function-for-immutable-struct-containing-mutable-vectors/68326 "2021-09-17T18:24:45Z")

</div>

Suppose I have an immutable struct A containing immutable fields as well as a (mutable) vector of immutable objects (such as Int64 objects), struct A x::Int y::Vector{Int} end Does Julia automatically define se…

---

## [Structure type + in place modification of columns of arrays](https://discourse.julialang.org/t/structure-type-in-place-modification-of-columns-of-arrays/64275)

<div class="topic-metadata">

**Author:** [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Replies:** 4\
**Last updated:** [July 8, 2021, 6:35pm UTC](https://discourse.julialang.org/t/structure-type-in-place-modification-of-columns-of-arrays/64275 "2021-07-08T18:35:24Z")

</div>

In this post, I have two questions I am not sure are completely separate or not, so I ask them both here. I define a structure and a substructure that has a field B which is an array of Distributions. It can be univ…

---

## [Control where mutables are allocated in a very special case: a large number of mutables of the same type](https://discourse.julialang.org/t/control-where-mutables-are-allocated-in-a-very-special-case-a-large-number-of-mutables-of-the-same-type/63971)

<div class="topic-metadata">

**Author:** [@zsunberg](https://discourse.julialang.org/u/zsunberg)\
**Replies:** 11\
**Last updated:** [July 4, 2021, 5:41am UTC](https://discourse.julialang.org/t/control-where-mutables-are-allocated-in-a-very-special-case-a-large-number-of-mutables-of-the-same-type/63971 "2021-07-04T05:41:36Z")

</div>

In my research, I often find myself incrementally creating large graphs or trees where each node has some properties that might get updated later. Semantically, it makes the most sense for each node to be represented by…

---

## [DataFrames groupby() by a column of mutable custom type](https://discourse.julialang.org/t/dataframes-groupby-by-a-column-of-mutable-custom-type/62632)

<div class="topic-metadata">

**Author:** [@AndreLC](https://discourse.julialang.org/u/AndreLC)\
**Replies:** 4\
**Last updated:** [June 9, 2021, 4:44pm UTC](https://discourse.julialang.org/t/dataframes-groupby-by-a-column-of-mutable-custom-type/62632 "2021-06-09T16:44:07Z")

</div>

Here is a small example: mutable struct Mystring str::String end using DataFrames strs = \["a","b","a","b"\]; mystrs = Mystring.(strs); df = DataFrame(col\_str=strs, col\_mystr=mystrs); println(df) println(groupby(df…

[Next page](https://discourse.julialang.org/tag/mutable-structure/501.md?match_all_tags=true&page=1&tags%5B%5D=mutable-structure)
