Skip to content

Instantly share code, notes, and snippets.

@nucular
Last active April 28, 2019 21:18
Show Gist options
  • Save nucular/7d0a2baf07c25c26768e to your computer and use it in GitHub Desktop.
Save nucular/7d0a2baf07c25c26768e to your computer and use it in GitHub Desktop.
Prototype-based class constructor for Lua in <30 unminified lines (5 when minified to 80-char lines)

Tiny Lua class implementation

This is a prototype-based class constructor for Lua in <30 unminified lines (5 when minified to 80-char lines). It was designed to be included in other library projects, especially for inclusion into single files.

It is released to the public domain under the Unlicense, so feel free to use it, even without a disclaimer, I don't really care.

I consider the interface to be quite intuitive, it is explained further inside the demo below.

-- class(prototype, inherited classes...) -> prototype with metatable set
local Being = class({})
local Animal = class({}, Being)
local Quadruped = class({})
-- Multiple inheritance
local Dog = class({
-- prototype.init is called as the constructor
-- It can return things by the way
init = function(self, name)
self.name = name
end
}, Animal, Quadruped)
-- Prototypes can be modified
function Dog:speak()
print(self.name .. " barks")
end
local Chair = class({}, Quadruped)
function Chair:speak()
error("Chairs can't speak")
end
-- Instantiation through calling the class
local dog = Dog("Bailey")
local chair = Chair()
-- The metatables of the instance and class expose the full prototype list
assert(getmetatable(dog).prototypes[1] == Dog) -- {Dog, Animal, Quadruped}
-- class.inherits(other class) -> true or false
assert(getmetatable(dog).prototypes[1].inherits(Quadruped))
-- Here's some helpful functions
function classof(instance)
return getmetatable(instance).prototypes[1]
end
-- This doesn't work for multiple inheritance
function super(self)
return getmetatable(self).prototypes[2]
end
local function class(prototype, ...)
local prototypes = {prototype, ...}
return setmetatable(prototype, {
__index = {
prototypes = prototypes,
inherits = function(proto)
for i, p in ipairs(prototypes) do
if p == proto then return true end
end
return false
end
},
__call = function(...)
local instance = setmetatable({}, {
prototypes = prototypes,
__index = function(table, key)
for i, p in ipairs(prototypes) do
if p[key] then
return p[key]
end
end
end
})
return instance, instance.init and instance:init(...) or nil
end
})
end
return class
local function class(a,...)local b={a,...}return setmetatable(a,{__index={
prototypes=b,inherits=function(c)for d,e in ipairs(b)do if e==c then return true
end end;return false end},__call=function(...)local f=setmetatable({},{
prototypes=b,__index=function(g,h)for d,e in ipairs(b)do if e[h]then return e[h]
end end end})return f,f.init and f:init(...)or nil end})end
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment