Difference between revisions of "32 lines of goodness"
Ishkabible (talk | contribs) (Created page with "{{#set:Name=32 lines of goodness}} {{#set:LOVE Version=0.7.2}} {{#set:Description=32 lines of goodness is a small OO library that uses a domain specific language to make a neat s...") |
m |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{notice|This library has problems when a two classes inherit from the same base class. Check the thread for more details.}} |
− | |||
− | |||
− | == The | + | == The library == |
<source lang="lua"> | <source lang="lua"> | ||
local mt_class = {} | local mt_class = {} | ||
− | function mt_class:extends( | + | function mt_class:extends(parent) |
− | self.super = | + | self.super = parent |
− | setmetatable(mt_class, {__index = | + | setmetatable(mt_class, {__index = parent}) |
− | + | parent.__members__ = parent.__members__ or {} | |
return self | return self | ||
end | end | ||
Line 39: | Line 37: | ||
</source> | </source> | ||
− | + | That's all there is to it! Just 32 lines! | |
== Usage == | == Usage == | ||
− | + | Slap the above library in a file of your choose (maybe "32log.lua") and include it in your code using the <code>require</code> function. | |
− | + | The basic syntax is as follows: | |
<source lang="lua"> | <source lang="lua"> | ||
Line 53: | Line 51: | ||
</source> | </source> | ||
− | + | Once a class has been created you can create new instances of the class as follows: | |
<source lang="lua"> | <source lang="lua"> | ||
Line 59: | Line 57: | ||
</source> | </source> | ||
− | + | If you create a method named <code>__init</code> it can be used to as a constructor with new. | |
<source lang="lua"> | <source lang="lua"> | ||
Line 79: | Line 77: | ||
</source> | </source> | ||
− | + | Whatever value you set a member to in the definition, it will act as a default value for the member. This works well with values like numbers and strings where they are always copied by value but tables can get a little tricky: | |
<source lang="lua"> | <source lang="lua"> | ||
Line 91: | Line 89: | ||
</source> | </source> | ||
− | + | Classes inherit their parent's default member values and meta-methods. You can also call a parent's method that was overloaded in the derived class using the <code>super</code> member: | |
<source lang="lua"> | <source lang="lua"> | ||
Line 100: | Line 98: | ||
class "Derived" : extends(Base) {} | class "Derived" : extends(Base) {} | ||
function Derived:foobar() | function Derived:foobar() | ||
− | self. | + | self.super.foobar(self) |
print("bar") | print("bar") | ||
end | end | ||
</source> | </source> | ||
+ | == See Also == | ||
+ | Post any questions you might have in the | ||
+ | [http://love2d.org/forums/viewtopic.php?f=5&t=3344 original post]. | ||
− | == | + | {{#set:Name=32 lines of goodness}} |
− | + | {{#set:LOVE Version=0.7.2}} | |
− | [ | + | {{#set:Description=32 lines of goodness is a small OO library that uses a domain specific language to make a neat syntax so OO is easy.}} |
+ | {{#set:Keyword=Class}} | ||
+ | [[Category:Libraries]] | ||
+ | == Other Languages == | ||
+ | {{i18n|32 lines of goodness}} |
Latest revision as of 14:49, 15 December 2019
This library has problems when a two classes inherit from the same base class. Check the thread for more details. |
Contents
The library
local mt_class = {}
function mt_class:extends(parent)
self.super = parent
setmetatable(mt_class, {__index = parent})
parent.__members__ = parent.__members__ or {}
return self
end
local function define(class, members)
class.__members__ = class.__members__ or {}
for k, v in pairs(members) do
class.__members__[k] = v
end
function class:new(...)
local newvalue = {}
for k, v in pairs(class.__members__) do
newvalue[k] = v
end
setmetatable(newvalue, {__index = class})
if newvalue.__init then
newvalue:__init(...)
end
return newvalue
end
end
function class(name)
local newclass = {}
_G[name] = newclass
return setmetatable(newclass, {__index = mt_class, __call = define})
end
That's all there is to it! Just 32 lines!
Usage
Slap the above library in a file of your choose (maybe "32log.lua") and include it in your code using the require
function.
The basic syntax is as follows:
class "ClassName" : extends(BaseClassName) {
memberName = nonNilValue;
}
Once a class has been created you can create new instances of the class as follows:
local myInstance = ClassName:new()
If you create a method named __init
it can be used to as a constructor with new.
class "Vector" {
x = 0;
y = 0;
z = 0;
}
function Vector:__init(x, y, z)
self.x = x
self.y = y
self.z = z
end
function Vector:print()
print(self.x, self.y, self.z)
end
local vec = Vector:new(1, 0.5, 0.25)
vec:print()
Whatever value you set a member to in the definition, it will act as a default value for the member. This works well with values like numbers and strings where they are always copied by value but tables can get a little tricky:
class "Foo" {
bar = {};
}
local foo = Foo:new()
foo.bar["foobar"] = 10;
local foo2 = Foo:new()
print(foo2.bar["foobar"])
Classes inherit their parent's default member values and meta-methods. You can also call a parent's method that was overloaded in the derived class using the super
member:
class "Base" {}
function Base:foobar()
print("foo")
end
class "Derived" : extends(Base) {}
function Derived:foobar()
self.super.foobar(self)
print("bar")
end
See Also
Post any questions you might have in the original post.
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info