Created
April 14, 2022 17:07
-
-
Save svermeulen/a08d5d376d3296cb13ad74dfcd714adc to your computer and use it in GitHub Desktop.
This file contains 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
local record Class | |
__name:string | |
__init:function(self:Class, ...:any) | |
end | |
function setup_as_class(class:Class, name:string):any | |
class.__name = name | |
setmetatable( | |
class, { | |
__call = function(_, ...):any | |
local instance = setmetatable({ __class = class }, { __index = class }) | |
if class.__init ~= nil then | |
class.__init(instance, ...) | |
end | |
return instance | |
end, | |
} | |
) | |
end | |
---------------------- | |
local record Foo | |
_bar:string | |
metamethod __call: function(self: Foo, bar:string): Foo | |
end | |
function Foo:__init(bar:string) | |
self._bar = bar or "asdf" | |
end | |
function Foo:get_bar():string | |
return self._bar | |
end | |
setup_as_class(Foo, "Foo") | |
---------------------- | |
local foo1 = Foo() | |
print(foo1:get_bar()) | |
local foo2 = Foo("zxcv") | |
print(foo2:get_bar()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment