lua-users home
lua-l archive

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


It.s not clear from the doc about which variable is created the first and which one will shadow the other ones made unreachable in scope.

The same could be said about the declaration in a single statement:

local a,a=1,2

Or the single assignment statement of a list:

a,a = fun()

or:

a,a = ...

I think we can expect the assignments of variabkes being performed from the leftmost variable to the rightmost one which wins.





Le mar. 27 nov. 2018 à 22:10, Thijs Schreijer <thijs@thijsschreijer.nl> a écrit :


> On 27 Nov 2018, at 19:53, Domingo Alvarez Duarte <mingodad@gmail.com> wrote:
>
> Hello !
>
> I just found when reading http://www.cse.chalmers.se/edu/year/2012/course/DAT150/lectures/plt-book.pdf on page 80 see bellow:
>
> ===
>
> To be really picky, the type checker of function definitions should also
> check that all variables in the parameter list are distinct. We shall see in the
> next section that variables introduced in declarations are checked to be new.
> Then they must also be new with respect to the function parameters.
>
> ===
>
> I decided to check lua and found that lua/luajit accept this without complain:
>
> ===
>
> function aa(a,a) return a end
>
> print(aa(1,2))  --> 2
>
> ===
>
> Is this the expected behavior or a bug ?
>
> Cheers !
>

Expected imo, you create 2 locals called a, the second one shadowing the first (assignments go left to right), so when you return, you return the second one.

Thijs