Closed mauro3 closed 5 years ago
Thanks for reporting this! Not sure, what the correct behavior would be. Return a copy of module A
with b
replaced by B(1)
? This is probably not possible in Julia. If you just want a modified copy of b
then I think your work around is actually the cleaner way to express this anyway. However I agree, the error message is not ideal.
If you just want a modified copy of
b
Yes, that's what I want. Modules ~can~ cannot be copied, so that option is not possible, thus it would probably be ok to just the modified copy of b
as there is no ambiguity.
Right, I agree it would be unambiguous. However I like the simplicity of @set obj...
returning a modified copy of obj
instead of doing something clever that is not syntactically obvious. If you like, you are very welcome to improve the error message. I think adding a method to setproperties
here would be enough:
function setproperties(m::Module, patch)
throw(ArgumentError("Cannot set properties $patch of module $m."))
end
Is the problem here that @set a.b.c = x
returns a modified copy of a
, not b
? I wonder if it makes sense to support "scoped assignment" syntax like @set a.b.(c = x)
to return a modified copy of b
. We can also extend this to a "batch" update syntax
a_modified = @set a.(
b = x;
c = y;
d = z;
)
Personally I sometimes do
b = a.b
@set b.c = x
but it is rare that I wish I could do this in one line. Does the desire for scoped assignment come up frequently for you? As for batch updates, this is a need that comes up very often for me. One case I run into frequently is inner constructors that enforce invariants. These are sometimes violated, if not all updates are carried out at the same time. E.g.
v = MyUnitVector(1,0)
@set! v.x = 0 # ouch this does not have norm 1!
@set! v.y = 1 # this again has norm 1
Right, it's not a big pain (especially now that there's @set!
). I'm mentioning it because I thought it might be what @mauro3 is asking. My motivation was the batch update, too. But I just realized that mixsing the notation for b = a.b; @set b.c = x
and the batch update was a bad idea because it then make it impossible to do the batch update for the nested locations.
I'd be interested in batch update too but not related to this. Thanks for all the inputs, I'll close now.
This does not work:
Work around:
No big deal, but I though I report.