lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Wed, Aug 10, 2011 at 01:12:30AM +0200, Ross Andrews wrote:
> By "order of assignment is undefined", does it mean that the order in which values are put in variables is undefined, or the order in which the right-hand side is evaluated is undefined?
> Here's why I'm asking: if it's the latter, then this:
> a, b = 1, 2
> Could mean either:
> a = 1
> b = 2
> or
> b = 2
> a = 1
No.  It means:

    tmp1 = 1; tmp2 = 2  -- either this
    tmp2 = 2; tmp1 = 1  -- or this, I'm not telling

followed by

    a = tmp1; b = tmp2  -- either this
    b = tmp2; a = tmp1  -- or this, I'm not telling

As for the examples alleging a compiler glitch: they illustrate 
programming malpractices.  Consider a less frivolous case 
than `a,a = 1,2`, one which could occur in an actual program:

    x[i], x[j] = 1, 2   -- no previous check that i and j are different

If i and j are legally allowed to be equal, the programmer should,
if only for the sake of maintainability of the program, test for that
case and explicitly do what should be done instead of relying on
whatever order of evaluation and assignment might be defined.

Similarly, in

    a, b = f(), f()

the function f should be one (like math.random) in which all that
matters are that a and b are assigned the values of different