lua-users home
lua-l archive

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


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

2009/11/1 RJ Russell <russellsprouts2@gmail.com>:
> Sorry, that code isn't the actual code, just a demonstration code that I
> made really fast. It was to demonstrate that the __newindex wasn't working
> on a function definition function foo()end. But now I rewrote another one,
> and it seems to work. I guess I was wrong. Thanks anyway.
>
> On Sat, Oct 31, 2009 at 7:17 PM, Luiz Henrique de Figueiredo
> <lhf@tecgraf.puc-rio.br> wrote:
>>
>> > However, this code doesn't seem to work:
>>
>> > do
>> >   local cdoc
>> >   local docs
>> >   local function newindex(G,k,v)
>> >     docs[k]=cdoc
>>
>> You need to initialize docs:
>>    local docs={}
>
>