Closed jw3126 closed 6 years ago
I am leaning towards making @set
be non mutating and adding an @set!
macro.
The difference between the two is that:
@set!
is mutating@set!
overwrites the bindingFor instance
@set! obj.field = val
will be equivalent to
let l = @lens _.field
obj = set(l, obj, val, EncourageMutation())
end
while
@set obj.field = val
will be equivalent to
let l = @lens _.field
set(l, obj, val, ForbidMutation())
end
What do you think @tkf ?
Yeah, I think that's good. I was thinking the same thing. One thing we need to mention in the document is that with @set!
and static struct with type parameters, type instability may happen. But I think it's OK since it's usual Julia caution when you want performance.
Closed by #25, at least for now :smile:
Currently
@set
will mutate things if possible:is this a good idea? It currently also means that
@set
can not change the type in the mutable situationThough that might be only a limitation of the current implementation.
Initially I thought it would be very useful to have
@set
mutate when possible to easier write generic code that does the right thing on both immutable and mutable objects. Now I am not so sure anymore. I had a couple of situations where I had a nested struct with a buried array somewhere and I wanted a modified copy instead of changing the original.@set
ever mutate anyting?@set!
macro that is mutating and@set
does not mutate?