lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.

[...]
>    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.

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

- --
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ "Sufficiently advanced incompetence is indistinguishable from
│ malice." -- Vernon Schryver
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkseaPoACgkQf9E0noFvlzgqrgCeLrcv4EH1Osh8XDyzuauBK3Ht
AXYAoMu1pFytqHTw1gp6vj17emSD7WXi
=1s7V
-----END PGP SIGNATURE-----