Skip to content

Instantly share code, notes, and snippets.

@appgurueu
Created August 22, 2021 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appgurueu/155d9b3165eb3d29ecd113952d88e07d to your computer and use it in GitHub Desktop.
Save appgurueu/155d9b3165eb3d29ecd113952d88e07d to your computer and use it in GitHub Desktop.
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