lua-users home
lua-l archive

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


Philip Bock wrote:
>
> Is there any way for an anonymous function to call itself?

Only via the debug interface.

But your problem is easier:

> For example, if I'm writing a table serializer, like this one:
>[...] 
> Is there any way to place the serialize_table function inside the 
> serialize function, so I don't have to pollute the global namespace,
> but still let it call itself?

Make it local - either at the global scope or within serialize.

> I suppose I could declare it local inside the serialize function,
> and then pass it as an argument to itself, but is there a less ugly
> way to do it?

You don't have to pass it as an argument.  A function is able to
access outer locals and the "local function" syntax makes sure that
the name is visible within the function.

  local function foo() ...foo()... end
  function bar() ...foo()... end

or, if foo wants to access locals or parameters of bar:

  function bar(xyz)
    local function foo() ...xyz...foo()... end
    ...foo()...
  end

Ciao, ET.