Re: Nil for Local Variable
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Nil for Local Variable
- From: Javier Guerra Giraldez <javier@...>
- Date: 2012年9月27日 22:55:43 -0500
On Thu, Sep 27, 2012 at 10:32 PM, Dong Feng <middle.fengdong@gmail.com> wrote:
> And then it says, if you declare a local variable with value "nil", that
> local variable still shadows the global ones (if there is one), meaning a
> local variable (which its own should be an item of some table) exists after
> assigned "nil".
AFAICT, local variables are statically assigned slots. so, yes, they
exist regardless of their value, even if it's nil.
but think for a while how surprising would it be if assigning nil
could make a local variable 'disappear'
do
local n = 'sneaking'
function f (x)
local n = x
print n
end
end
f(2) => 2
f(nil) => 'sneaking'
isn't it weird? you assign a value to a variable and find a different
value, depending on code outside the function!
--
Javier