lua-users home
lua-l archive

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


Omg, shadows? It is even worse: does what means if I shadow redefine table, it will be never garbage collected?
local a = {} -- this table will be never collected?
local a = nil -- shadow the previous definition


2013/8/23 Leo Razoumov <slonik.az@gmail.com>
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--




--
С уважением,
инженер-программист ООО "СТ-ПРЕМИУМ"
Рафис Ганеев
+79257371586
rafisganeyev@gmail.com