Created
April 24, 2020 06:12
-
-
Save eromatiya/550b642cd66e0d0107d33a5efd88edcc to your computer and use it in GitHub Desktop.
Lua __call methamethod example
This file contains hidden or 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
#!/usr/bin/env lua | |
meta_call = require('meta-call') | |
-- Index | |
str_table = { | |
'string1', | |
'string2', | |
'string3', | |
'string4' | |
} | |
-- Associative | |
name_tbl = { | |
friend0 = 'Anne', | |
friend1 = 'Allen', | |
friend2 = 'Ivan', | |
friend3 = 'Alecz', | |
friend4 = 'Anthony' | |
} | |
meta_call(str_table) | |
meta_call(name_tbl) |
This file contains hidden or 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
#!/usr/bin/env lua | |
mainTable = {} | |
function func(args) | |
args= args or {} | |
-- Associative | |
for i, friend in pairs(args) do | |
print(friend) | |
end | |
end | |
return setmetatable( | |
mainTable, | |
{ | |
__call = function(_, ...) | |
return func(...) | |
end | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment