2020-07-31 –, Red Track
We discuss the problem of updating immutable objects. The solutions presented are implemented in the Setfield.jl
package.
In Julia, some objects are mutable (Array
, mutable struct
, ...
), while others are immutable (Tuple
, struct
, ...
). Neither is strictly better than the other in every situation. However, immutability usually leads to code that is easier to reason about, for both humans and compilers. And therefore less buggy and more performant programs.
One convenience with mutability is, that it makes updating objects very simple:
spaceship.captain.name = "Julia"
The analogous operation in the immutable case is to create a copy of spaceship
, with just the captain's name changed to "Julia".
Just think for a moment, how would you achieve this? It is a serious obstacle to adopting immutables in practice. The title "Changing the immutable" refers to this and similar problems. There are various approaches, for instance, https://github.com/JuliaLang/julia/pull/21912 .
I would like to talk about this problem and present one solution, namely Setfield.jl
. In the spirit of Julia, it is dirt simple to learn:
@set spaceship.captain.name = "Julia"
yet keeps the most serious hackers happy (as it is built around the beautiful concept of lenses).
Mathematician and Julia enthusiast working in medical physics.