lua-users home
lua-l archive

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


At 06:54 PM 7/13/2011, Steve Litt wrote:
I know the require syntax would definitely work just right if I made the definition of Columns and Columns.new() in a separate module, but I was trying to find a way to have them in the same file but lower down.

At the time that you attempt to call any function via some variable, the function has to be stored in (referenced by, actually) that variable.

You cannot write this:

local foo
foo()
function foo() end

but the reason why is clearer if the third line were written in its equivalent (syntax sugar free) form:

local foo
foo()
foo = function() end

Now it should be clear that at the time of the attempt to call the value in foo, the name is known to be a local variable containing nil, but that the assignment of a function to it has yet to happen.

The same situation occurs when you attempt to forward declare your Columns object and its methods. You can declare that a local variable exists named Columns, but until you've stored something in it, you shouldn't be surprised that it holds the value nil.


Ross Berteig                               Ross@CheshireEng.com
Cheshire Engineering Corp.           http://www.CheshireEng.com/