Search code examples
structjuliaimmutabilitymutable

mutable fields in Julia struct


I couldn't find an answer in both stackoverflow and the Julia docs to the following "design problem":

Let's say I want to define the following object

struct Person
birthplace::String
age::Int
end

Since Person is immutable, I'm happy that nobody can change the birthplace of any Person created, nonetheless, this also implies that when time passes, I cannot change their age either...

On the other hand, if I define the type Person as

mutable struct Person
birthplace::String
age::Int
end

I can now make them age, but I don't have the safety I had before on the birthplace, anyone can access it and change it.

The workaround I found so far is the following

struct Person
birthplace::String
age::Vector{Int}
end

where obviously age is a 1-element Vector.
I find this solution quite ugly and definitely suboptimal as I have to access the age with the square brackets every time.

Is there any other, more elegant, way to have both immutable and mutable fields in an object?

Maybe the problem is that I am missing the true value of having either everything mutable or immutable within a struct. If that's the case, could you explain me that?


Solution

  • For this particular example it seems better to store the birthdate rather than the age, since the birthdate is also immutable, and it is simple enough to calculate the age from that information, but perhaps this is just a toy example.


    I find this solution quite ugly and definitely suboptimal as I have to access the age with the square brackets every time.

    Usually you would define a getter, i.e. something like age(p::Person) = p.age[1] that you use instead of accessing the field directly. With this you avoid the "ugliness" with the brackets.

    In this case, where we only want to store a single value, it is also possible to use a Ref (or possibly a 0-dimensional Array), something like:

    struct Person
        birthplace::String
        age::Base.RefValue{Int}
    end
    Person(b::String, age::Int) = Person(b, Ref(age))
    age(p::Person) = p.age[]
    

    with usage:

    julia> p = Person("earth", 20)
    Person("earth", 20)
    
    julia> age(p)
    20