lua-users home
lua-l archive

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



> On Behalf Of Asko Kauppi
> 
> Is there a simple "better way" to this?
> 
> When I have local functions that rely on each other, and the one used
> cannot be defined before the usage place (it's used at runtime,
anyhow)
> I do this:
> 
> 	local func2
> 
> 	local function func1()
> 		...
> 		func2()
> 		...
> 	end
> 
> 	...
> 	--local
> 	function func2()
> 		...
> 
> This is the "right" way to do it, right?
> 
> I don't like it too much, since 'func2' mustn't have "local" before
it.
>   If it does (which happens easily) the value within 'func1' will be
> 'nil', and things fall apart.  Of course, this is not noted until
> runtime, so bugs easily lurk in.
> 
> Also, the latter definition of 'func2' looks too much of a global.

You cannot declare functions in Lua. Local values will be concreted in
the function closure made. If you can't move func2 before func1 you'll
have to make a global variable that accesses func2. Global variables are
lazy evaluated so you can set them anytime before you need to use them.
Local variables are evaluated when they are parsed and the reference
made concrete.

[Haha! You might be able to do this if we had a compilation event which
matched up a missing function with one that you pre-declared (see last
email) ? ]

Nick