I know this from before, that you can modify mutable fields wrapped in an immutable struct, but this still caught me by surprise.
Maybe itâs the fact that the Dict inside the Set is so âhiddenâ, you donât even know itâs there. So Setseems very much to be mutable. Thereâs some disconnect between the implementation detail that causes Set to be immutable, and the observed interface which is that Setis mutable.
So ismutable isnât the right way to figure out the behaviour of Set. Could there be a Mutable trait or something along those lines, that tell you about the behaviour of a type, rather than its implementation?
function truly_immutable(T)
T === String && return true
T === Symbol && return true
body = T isa UnionAll ? T.body : T
ft = fieldtypes(body)
body.mutable && return false
isempty(ft) && return true
return all(truly_immutable, ft)
end
Indeed it isnât. Itâs for querying a property with a very specific meaning in Juliaâs type system â its docstring refers the reader to mutable composite types.
Technically it is possible, but it would make sense to think a bit about the application first, with examples. Personally I would prefer to design my code in a way that I donât have to think about this for objects I do not âownâ, and for the rest I know anyway.