LuaMeta - Metaprogramming Library for Lua

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Almia
Prole
Posts: 7
Joined: Mon Aug 05, 2013 7:56 am

LuaMeta - Metaprogramming Library for Lua

Post by Almia » 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

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)
Traits

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()
Suggestions will be much appreciated. Thank you!

Post Reply

Who is online

Users browsing this forum: MissDanish and 7 guests