Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go-inspired exports based on variable capitalization
local parent = getfenv(1)
local private = {}
local public = {}
setfenv(1, setmetatable({}, {__newindex = function(self, key, value)
local first = key:sub(1, 1);
((first >= "A" and first <= "Z") and public or private)[key] = value
end, __index = function(self, key)
local value = private[key]
if value ~= nil then
return value
end
local value = public[key]
if value ~= nil then
return value
end
return parent[key]
end}))
function privateFunc()
print"hello world"
end
function PublicFunc()
privateFunc()
end
assert(private.privateFunc and not private.PublicFunc)
assert(public.PublicFunc and not public.privateFunc)
return public
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment