Last active
May 14, 2017 22:19
-
-
Save jasonknight/ee129c0932bcbde22e4015d25e77eb13 to your computer and use it in GitHub Desktop.
Lua Object as a Function- https://repl.it/Hx2o/8
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
-- Lua object implemented with just functions. | |
function make(a,b) | |
return function(msg) | |
if msg == "a" then return a end | |
if msg == "b" then return b end | |
if msg == "mul" then | |
return function() | |
return a * b | |
end | |
end | |
if msg == "tostring" then | |
return function (str) | |
return string.format(str,a,b) | |
end | |
end | |
end | |
end | |
x = make(5,6) | |
y = make(42,3.14) | |
print(x("a")) | |
print(x("b")) | |
print(y("a")) | |
print(x("mul")()) | |
print(y("tostring")("<%d,%d>")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment