[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Undocumented/Buggy Behavior with Function Definitions
- From: RJ Russell <russellsprouts2@...>
- Date: Mon, 2 Nov 2009 14:46:15 -0800
On Mon, Nov 2, 2009 at 12:04 PM, Eike Decker <email@example.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.
> local function bar() return bar end
> will return the function itself as this a special case of assignment.
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