lua-users home
lua-l archive

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


On Fri, Aug 23, 2013 at 4:32 AM, Rafis DoctorInfo
<rafisganeyev@gmail.com> wrote:
> Why Lua 5.1 allows to define local variables more than once and not throws
> error?
> local a = 1
> local a = a or 2
> print(a) -- will print "1"
>
> function f(a)
>   -- we don't see it, but variable `a` is already defined in function's
> scope
>   -- and now we still can define local variable with same name `a`
>   local a = a or 4
>   print(a)
> end
> f(3) -- will print "3"
>
> --
> rafis

Lua defined two local variables for you with the same name 'a'. The
second one shadows the first.

--Leo--