I'm having a little trouble figuring out how to store local functions on files that are required. I'm not sure how to word this, so I wrote some scripts to explain what I mean (also attached as a .zip):
local foo = {}
function foo.foo()
bar()
end
local function bar()
assert(nil, "foobar!")
end
return foo
Maybe this is already obvious but foo.foo() will return an error because it refers to bar() - a function that is local to foobar.lua. require would just scurry past it, but I'd like to keep it local because the main script has no direct use for bar, only the foo table would. Is there any way to retain this type of structure, keep the function local and make everything work? If not, could I get some alternate ways to make this work (preferably while keeping bar a local function)?
just move the definition of bar() above that of foo.foo(). this will turn foo.foo() into a closure with bar() as an upvalue, which allows it to be used by foo.foo() even though it is no longer in scope by the time you call it