lua-users home
lua-l archive

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


David Given <dg@cowlark.com> dixit:

> spir wrote:
> [...]
> > output = function(thing, level)
> [...]
> > Is here 'level' local in the case it was not defined inthe calling expression?
> 
> Parameters are always local; if a value isn't specified when your
> function was called, then it'll be initialised to nil. It's the
> *declaration in the function* that matters, not how your function was
> called.


Thank you very much, this is exactly what I was wondering.
(Actually, I find there is a bit of ambiguity around the sense of 'nil', or maybe I haven't it right yet. I would like a value that means *only* 'undef' or 'unkown in reachable scope', in a way acting like #ifdef, but that we cannot even assign to a name.)

> [...]
> >    if not level then local level = 1
> 
> In fact, this won't do what you expect. 'local' defines variables in the
> enclosing scope --- which here is the then...end block.

Right, I was caught! See how bad it is to write a conditionned block without newline/indent... I did not even realise there is a scope in there.

> do
>   local var = 1
>   if true then
>     local var = 2
>     print(var) -- outputs 2!
>   end
>   print(var) -- outputs 1!
> end

Thank you again.

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/