Last active
September 5, 2015 13:29
-
-
Save Yoplitein/9ed9534d2a1ceabe27a8 to your computer and use it in GitHub Desktop.
Helper function to declare a class in Lua with a Python-esque constructor syntax
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
function class(ctor, members) | |
local Class = {} | |
Class.__index = Class | |
if members then | |
if type(members) ~= "table" then | |
error("Passed non-table type as class members") | |
end | |
for k, v in pairs(members) do | |
if k == "__index" then | |
error("Cannot overwrite __index on class") | |
end | |
Class[k] = v | |
end | |
end | |
setmetatable( | |
Class, | |
{ | |
__call = function(metatable, ...) | |
local instance = {} | |
setmetatable(instance, Class) | |
ctor(instance, ...) | |
return instance | |
end | |
} | |
) | |
return Class | |
end | |
--Example usage | |
local Vector = class( | |
function(self, x, y) | |
self.x = x | |
self.y = y | |
end, | |
{ | |
__eq = function(self, other) | |
return | |
self.x == other.x and | |
self.y == other.y | |
end | |
} | |
) | |
local vec1 = Vector(0, 0) | |
local vec2 = Vector(1, 1) | |
assert(vec1 == Vector(0, 0)) | |
assert(vec2 == Vector(1, 1)) | |
assert(vec1 ~= vec2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment