[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: local pre-declarations
- From: "Nick Trout" <nick@...>
- Date: Tue, 31 Aug 2004 17:32:21 -0700
> 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,
> I do this:
> local func2
> local function func1()
> 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
> 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
[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) ? ]