Created
August 22, 2021 10:29
-
-
Save appgurueu/155d9b3165eb3d29ecd113952d88e07d to your computer and use it in GitHub Desktop.
Go-inspired exports based on variable capitalization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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