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?
incrementage
function that creates a new object with the right age? e.g.incrementage(p::Person) = Person(p.birthplace, p.age+1);
– Replevin