lua-users home
lua-l archive

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


On Sun, Sep 25, 2005 at 06:00:38PM +0200, Enrico Tassi wrote:
> What should we expect from this code?
> 
> x = 1
> function f()
>   x = x + 1
>   return x
> end
> 
> a,b,a = f(), f(), f()
> print(a,b,x)
> 
> I checked a bit the Lua 5.0.2 manual and there is no clarification,
> except for the fact that f() is called 3 times.
> 
> The result of the execution is 2,3,4.
> I think there are 2 possibilities:
> - after the f() calculation assignments are performed from the right
>   to the left
> - assignments are performed from the left to the right but there is a 
>   check for duplicate variable name
> 
> I guess the first.

Yes. The assignments are made in the order the values are popped off
the stack.

> In any case... isn't this a good place to generate an error? Is this
> check considered too heavy or not so interesting?

Consider the idiom

local _,_,foo = lots_of_results()

The _ is not special in Lua; it's just used for garbage by convention.

-- Jamie Webb