Created
February 11, 2017 15:10
-
-
Save jamesrswift/a6841e522957b3f38f3b1120a33f20e5 to your computer and use it in GitHub Desktop.
Promise functions to native libraries, that can be redefined at any point
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
--[[ | |
Virtual functions - Lua | |
]] | |
local vfunc = { } | |
function PromiseVirtualFunction( sFunctionName ) | |
return function( ... ) | |
if (vfunc[sFunctionName]) return vfunc[sFunctionName]( ... ) end | |
error( string.format( "Promised function %s was nil", sFunctionName ) ) | |
end | |
end | |
function DefinePromisedFunction( sFunctionName, func ) | |
vfunc[sFunctionName] = func | |
end | |
-- Example use | |
timer.Simple( 1, PromiseVirtualFunction( "MyPromisedFunction" )) | |
DefinePromisedFunction( "MyPromisedFunction", function() print("Hello from promised function") end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment