lua-users home
lua-l archive

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


Is there any way for an anonymous function to call itself?

For example, if I'm writing a table serializer, like this one:

function serialize_table(table, outfile, tab_depth)
	outfile:write("{")

	local indent = string.rep("\t", tab_depth)

	for k, v in pairs(table) do
		if (type(v) == "number" or type(v) == "string")
			outfile:write(indent..k.." = "..v)
		elseif (type(v) == "table")
			serialize_table(table, outfile, tab_depth + 1)
		end
	end

	outfile:write("}")
end

function serialize(table, filename)
	local outfile = io.open(filename, "w")

	outfile:write("return ");
	serialize_table(table, outfile, 1)
	outfile:close()
end

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? 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?

Thanks, Philip Bock