LuaMeta - Metaprogramming Library for Lua
Posted: Thu Oct 18, 2018 5:14 pm
Hello there,
I am going to share this experiment of mine to the LOVE community. The library is made for experimental purposes, but I think you can still find uses for it.
Repo:
https://github.com/LXSMNSYC/luameta
Current features include:
- Class
- Trait
- Namespace
further information included in the repo's README.
here are some previews
Classes
Traits
Suggestions will be much appreciated. Thank you!
I am going to share this experiment of mine to the LOVE community. The library is made for experimental purposes, but I think you can still find uses for it.
Repo:
https://github.com/LXSMNSYC/luameta
Current features include:
- Class
- Trait
- Namespace
further information included in the repo's README.
here are some previews
Classes
Code: Select all
class "vec2"
: constructor (function (self, x, y)
self.x = x or 0
self.y = y or 0
end)
: meta {
__tostring = function (self)
return "vec2("..self.x..", "..self.y..")"
end
}
class "vec3" : extends "vec2"
: constructor (function (self, x, y, z)
self.z = z or 0
end)
: meta {
__tostring = function (self)
return "vec3("..self.x..", "..self.y..", "..self.z..")"
end
}
class "vec4" : extends "vec3"
: constructor (function (self, x, y, z, w)
self.w = w or 0
end)
: meta {
__tostring = function (self)
return "vec4("..self.x..", "..self.y..", "..self.z..", "..self.w..")"
end
}
local a = vec2(1, 2)
local b = vec3(1, 2, 3)
local c = vec4(1, 2, 3, 4)
print(a) -- vec2(1, 2)
print(b) -- vec3(1, 2, 3)
print(c) -- vec4(1, 2, 3, 4)
print(b:super()) -- vec2(1, 2)
print(c:super()) -- vec3(1, 2, 3)
print(c:super():super()) -- vec4(1, 2, 3, 4)
Code: Select all
trait "exampleTraitStatic"
: static {
say = function (...)
print(...)
end
}
trait "exampleTraitMethod"
: method {
say = function (self)
print(self.intro .. " " .. self.msg)
end,
setMessage = function (self, msg)
self.msg = msg
end
}
trait "exampleTrait"
: implements "exampleTraitStatic"
: implements "exampleTraitMethod"
class "test"
: constructor (function (self, intro)
self.msg = "default string"
self.intro = intro
end)
: implements "exampleTrait"
: method {
repeatMessage = function (self, n)
self.msg = string.rep(self.msg, n)
end
}
: meta {
__tostring = function (self)
return self.msg
end
}
local a = test("Hello, the message is")
test.say("this is a test")
a:say()
a:setMessage("hello world")
a:say()
a:repeatMessage(2)
a:say()