[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Assignment statement does not evaluate all its expressions first
- From: Francisco Olarte <folarte@...>
- Date: Sun, 12 Sep 2021 17:43:10 +0200
On Sun, Sep 12, 2021 at 11:58 AM Xmilia Hermit <email@example.com> wrote:
> node.next, node = new, new
> could also not evaluate the node from node.next before node is assigned
> to new, however, this case is explicitly checked in
> Why even do this check in the first place if the prefix-expression parts
> do not need to be evaluated before the assignments?
> Further, from https://www.lua.org/manual/5.4/manual.html#3.4 I would
> assume that prefixexp are also expressions.
Yeah, it may weel be that, I'm not really that familiar.
I think the docs try to point you can use multiple asignementes to do
temp-less swap/rotate tricks in things like a,b=b,a or a,b,c=b,c,a,
but this levels of detail is justo too much for me.
> Thus the code
> i = 3
> i, a[i] = i+1, 20
> sets a to 20, without affecting a because the i in a[i] is
> evaluated (to 3) before it is assigned 4.
I suppose it does, but haven't dug too much on that. I mean, both your
examples seem to be using multi-assignement where it is not needed (
node.next=new, node=new and a[i]=20, i=i+1 seem clearer for me than
that ) and I have not found much room for confussions in real use
cases, may be because I only use multi-assign where I need it.
> It would be nice to know how this is meant to work as the my example is
> a bit contrived, however, this could happen in a more complex __newindex
> metamethod which for example calls to other hook functions.
It could be, but I think any _newindex which makes things like
node.x, node.y=1,2 work "weirdly", as your example, should be
thoroughly documented and kept for well-trained users.
Anyway, I agree the documentation could use some work stating that
side effects may alter things. I just do not know how to suggest one.