Skip to content

Instantly share code, notes, and snippets.

@eromatiya
Created April 24, 2020 06:12
Show Gist options
  • Save eromatiya/550b642cd66e0d0107d33a5efd88edcc to your computer and use it in GitHub Desktop.
Save eromatiya/550b642cd66e0d0107d33a5efd88edcc to your computer and use it in GitHub Desktop.
Lua __call methamethod example
#!/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)
#!/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