> I'm not sure what you are saying with that code. There is no possible way > to define the function mutate_immutable!! such that it modified a or c.
This does it: immutable A; a::Int end a = A(1) c = a A.mutable = true a.a = 99 A.mutable = false @show a, c #(A(99),A(99)) a===c===A(99) # true I don't think a trick like this can be pulled with an Int. Although putting it into a function errors. I also tried the pointer trick Simon was using but couldn't get it to work. > You example works the same with Ints, but might be easier to see: > > a=1 > c=a > mutate_immutable!(a,5) > c === a === 1 # true > > function mutate_immutable(x,y) ... end > On Tue, Feb 10, 2015 at 5:22 PM Mauro <[email protected]> wrote: > >> @Stefan, thanks that was a good read. I liked the example. >> >> > The take away should be that you can't mutate an immutable any more than >> > you can mutate an integer. If you have an array [1,2,3] and you assign 4 >> to >> > the 2, you don't change the value of 2, you change what value exists in >> the >> > second position of the array. >> >> This applies to isbits immutables, right? Then the only surprise could >> be when two bindings point to the same immutable: >> >> julia> immutable A; a::Int end >> >> julia> a = A(1) >> A(1) >> >> julia> c = a >> A(1) >> >> julia> mutate_immutable!!(a, 5) >> >> julia> c >> A(5) >> >> Which couldn't happen with an Int. >> >> > As Stefan just mentioned, we do need to add some mechanisms for more >> easily >> > creating them from existing ones. >> > On Tue, Feb 10, 2015 at 9:41 AM Mauro <[email protected]> wrote: >> > >> >> Thanks Lex, Jameson and Michael for this interesting discussion. I read >> >> it a few times but still cannot quite follow: >> >> >> >> Is the take-home that it is ok to mutate immutables? No repercussions >> >> from Julia itself, just confused library users? This is not what the >> >> manual suggests: >> >> http://docs.julialang.org/en/latest/manual/types/# >> >> immutable-composite-types >> >> >> >> Or is it just ok to mutate immutables stored in a mutable container? >> >> >> >> Or is the take-home that for library interface code it's ok to mutate >> >> immutables because there is no other way to mirror C-structs? >> >> >> >>
