5

I noticed that when a struct is declared, its fields are immutable by default unless the mutable keyword is used.

What is the reason for this? Is it good practice to use mutable structs?

3
  • 2
    In general, immutability provides safety against accidental mutation as well as allows the compiler to make more assumptions. Commented Jan 14, 2020 at 16:38
  • Have tryed you any research? Your question needs improving please read How do I ask a good question? Commented Jan 14, 2020 at 16:43
  • 8
    You've made the devastating choice of tagging this question with a general tag that has brought it to the attention of the SO community outside of Julia, whose favorite thing to do on SO is to vote to close questions. If you'd left that struct tag off, this would be open and answered. Commented Jan 14, 2020 at 16:53

2 Answers 2

10

This is discussed in this PR to Julia. I'll just quote from Jeff Bezanson:

  • We should steer people to immutable types by default. Immutable is generally better and faster, has better default behavior with ===, and it's really pretty rare to need to update object fields. Base has many more immutable types than mutable, and in making this change it seemed to me that even more types could be made immutable. But type Foo looks more natural than immutable Foo, so type tends to be the default choice. This change reverses that, with struct Foo being more natural, and immutable. Writing mutable struct Foo makes you ask "does this really need to be mutable?", which is a good thing!
  • Immutable struct types are the closest thing we have to value type structs in other languages. We inline them in arrays in at least some cases, and being immutable makes it much harder to tell whether they are value or reference types.
  • FWIW, Rust also uses struct and makes them immutable. Not that we need to copy Rust, but it adds confidence that this is a reasonable thing to do.

In short, immutable types are more performant, because they can be stack allocated and the compiler can reason better about them. They are also easier for humans to reason about, since they can't change from under you.

Sign up to request clarification or add additional context in comments.

3 Comments

+1. Newbie here. What are use cases for "mutable struct"? I ask because when I look at source code for inspiration, I see a lot of them. " When I search github for language:julia struct -"mutable struct" I find 40k code entries, and when I search language:julia "mutable struct" I find 37k entries. So performance and readability considerations do not seem to make a big impact on what users are choosing.
@PatrickT only mutable structs can be mutated. Sometimes you really do want the notion of "the same object, but it can change". Also: some things have be heap-allocated if they need to interact with the GC, or be mutable (such as files or RNG states). For very large structs, it can also be faster to keep them on the heap. In general, you should just use immutable types unless you explicitly want mutability.
Thanks Jakob. How would I know if I want mutability? I've read things like you can have a Vector or Matrix which is not mutable, but you can still change its elements without a penalty, now I don't know if I understood correctly, but it sounded like it would be a good strategy to store everything in large non-mutable containers and you would still be able to mutate elements. Did I misunderstand? Do you know a good place to start reading on this? Thanks!
1

Julia has this feature for a few reasons:

  1. Structs can be packed efficiently into arrays and in some cases, the compiler can avoid allocating space for them entirely.

  2. Code that uses immutable structs can be easier to process for the reader.

  3. The type's invariants will never be violated.

Generally, using mutable structs is bad practice because changing an object's internals can lead to violation of its invariants. Structs should only be declared with the mutable keyword when absolutely necessary.

1 Comment

I would not say it's bad practice to use them, neither to only use them when it's absolutely required. The choice should be made with care, though, and if you haven't thought about the trade-offs, defaulting to immutability is a good strategy to either make it safe, or force you to think about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.