lua-users home
lua-l archive

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


In fact what is really equivalent is:
  function c() end
and
  local c = function() end
Note the additional "local" keyword needed for the equivalence!

And in all cases the first one does not declare a "global" function. All functions are declared locally (in the closure where it is defined). There's no "global" functions at all in Lua.



Le lun. 19 nov. 2018 à 13:56, Philippe Verdy <verdy_p@wanadoo.fr> a écrit :
your example is wrong.
You've tested only the "function name()" case that does not hides any "name" variable in scope.
I'm sure that:
  c=1; do function c() end; end print(c)
  --> 1
and:
  c=1; do c=function() end; end print(c)
  --> function: 0xnnnn
are NOT equivalent.
- the first one (function c() end) declares a new local variable "c" in all cases,
- the second one (c = function() end) does NOT declare a new local variable but overwites the value of the existing variable "c" in scope.



Le lun. 19 nov. 2018 à 13:07, Francisco Olarte <folarte@peoplecall.com> a écrit :
On Sun, Nov 18, 2018 at 8:03 PM Philippe Verdy <verdy_p@wanadoo.fr> wrote:
> So "function f(x)... end" is not completely equivalent to "f= function(x) ... end": the first one declares a new local variable named "f", the second overwrites an existing variable "f" in scope, or creates a new local variable.

Are you sure? The manual (
https://www.lua.org/manual/5.3/manual.html#3.4.11 ) says it's
completely equivalent and a simple test:

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> a=1;function tst() local b; function a() end; function b() end; function c() end; print('a',a,'b',b,'c',c,'Z'); end; print('a',a,'b',b,'c',c,'Z'); tst(); print('a',a,'b',b,'c',c,'Z');
a    1    b    nil    c    nil    Z
a    function: 0x9da150    b    function: 0x9da0c8    c    function:
0x9bfdb8    Z
a    function: 0x9da150    b    nil    c    function: 0x9bfdb8    Z
Seems to contradict your "or creates a local variable" ( c is alive
after tst(), so it should be global )

Francisco Olarte.