Created
August 31, 2012 03:41
-
-
Save spion/3548825 to your computer and use it in GitHub Desktop.
Wrap old-style lua functions to support both old and new styles
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 table = require('table'); | |
function wrap(cb, fn) | |
return function(...) | |
local arg = {...}; | |
if (type(arg[cb]) == 'function') then | |
return fn(...); | |
else | |
local myargs = {...}; | |
table.insert(myargs, cb, 1); | |
return function(callback) | |
myargs[cb] = callback; | |
return fn(unpack(myargs)); | |
end | |
end | |
end | |
end | |
local test = wrap(3, function(a, b, cb) | |
cb(a + b); | |
end); | |
print("old style"); | |
test(2, 2, function(res) print(res) end); | |
print("new style"); | |
test(2, 2)(function(res) print(res) end); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment