[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: modules and locals
- From: Jonathan Castello <twisolar@...>
- Date: Mon, 23 Aug 2010 14:19:18 -0700
On Mon, Aug 23, 2010 at 2:11 PM, Andreas Matthias
<andreas.matthias@gmail.com> wrote:
> Calling foo() from the following module results in an
> "attempt to call global 'bar' (a nil value)":
>
> module(...)
>
> function foo()
> bar()
> end
>
> local function bar()
> end
>
>
> But it's running through if I
> a) define bar() before foo() or
> b) define bar() to be non-local.
>
> What's going on here? I don't understand it.
>
>
> Ciao
> Andreas
>
>
bar() simply isn't defined at the time that foo() is defined. foo()
acts as a closure, capturing the variables the function uses that it
sees, at that particular point in time. Since bar() isn't defined when
this is done, it assumes it's a global.
Think about it, you can't do this either:
function lolwat()
local func = function() print(val) end
local val = 42
func()
end
And that's effectively exactly what your code is trying to do.
~Jonathan