Re-reading the thread I believe what is being asked for is essentially a copy on write semantic for replacing an immutable item in a collection with a slightly modified original, not updating the immutable. So this isn't mutation at all ?
In the case of an in-lined object it is safe to be an in-place update, in the case of a referenced immutable (non bits types) a new copy would have to be made with the modification and the reference updated. Otherwise I don't see how you can trust that there are not additional references to the same immutable as per @Mauro. When you give people pointers ... immutability runs away... good job we don't have string interning like Java function change( x, y ) unsafe_copy!(pointer(x), pointer(y),3) end x = "hello" y = "world" change( x,y ) x == "worlo" On Tuesday, February 10, 2015 at 5:22:16 PM UTC-5, Mauro 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] <javascript:>> > 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? > >> > >
