Function scope assignment

do
	local a do
		local function b()
			return 1
		end
		
		a = function()
			print(b())
		end
	end
end

print(a, b) -- function: 0xhexaddress nil

Let’s say you want to have a function use other sub-functions but make the sub-functions private from the rest of the code. But I want to make that function retain its scope. So I do this but a is assigned global BUT the variable is local. What do I do?

Your example should print nil, nil. Your local a goes out of scope at the end of your first do end block, so accessing it from outside won’t yield you any results.

As for having “private” functions for your other functions to depend on, you can do the following:

local your_func_name
do
    local function helper_func()
        return 1
    end

    function your_func_name()
        print(helper_func())
    end
end

Note that doing function your_func_name() or your_func_name = function() both assign to the local variable.

Ah, never mind. I ran the code snippet in the command bar when I predefined the function a() prior, so I was confused when it showed that there was a new function.