lua-users home
lua-l archive

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


On Mon, Nov 2, 2009 at 12:04 PM, Eike Decker <zet23t@googlemail.com> wrote:
> It is not completely true that
> function f () end
> is translated to
> f = function () end
> For global variable assignments this might be true but not so for
> local variables.
>
> Sample case of corner case:
> local foo = "blah"
> local foo = function () return foo end
> print(foo()) -- prints "blah"
>
> The scope of the local variable is only starting after the assignment
> has been finished, so this code
>
> local bar = function () return bar end
> print(bar()) -- returns nil
>
> makes perfectly sense but is not what you might expecting to get.
> Contrary:
>
> local function bar() return bar end
>
> will return the function itself as this a special case of assignment.
>
> Greetings,
> Eike
>
I knew that for locals it was different, I was just worried about
global. I found the problem in my code (real code-not the one I posted
here) though. The function definition that I wanted to be detected by
a __newindex in _G's metatable was already defined in an earlier file,
so it wasn't detected. I thought that it showed that function foo()
definitions don't cause __newindex events, but it was just because it
wasn't a new index. That method of function documentation won't be
very effective anyway (though I think it looks best), I'll go back to
one on the wiki. Mine won't work in cases like local functions,
already defined variables, and functions not in the global
environment.