Created
April 14, 2022 00:34
-
-
Save svermeulen/2ebe995ad7e398dc7c707a1be0c87674 to your computer and use it in GitHub Desktop.
Lua Teal Interface Example
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 IFoo | |
bar: function(self:IFoo, integer):integer | |
end | |
-------------------- | |
local record Foo1 | |
bar: function(integer):integer | |
end | |
function Foo1:bar(value:integer):integer | |
return value + 1 | |
end | |
function Foo1.new(): Foo1 | |
return setmetatable({ } as Foo1, { __index = Foo1 } as metatable<Foo1>) | |
end | |
-------------------- | |
local record Foo2 | |
bar: function(integer):integer | |
end | |
function Foo2:bar(value:integer):integer | |
return value - 1 | |
end | |
function Foo2.new(): Foo2 | |
return setmetatable({ } as Foo2, { __index = Foo2 } as metatable<Foo2>) | |
end | |
-------------------- | |
-- local foo_type = 1 | |
local foo_type = 2 | |
local function create_foo():IFoo | |
if foo_type == 1 then | |
return Foo1.new() as IFoo | |
end | |
return Foo2.new() as IFoo | |
end | |
local foo = create_foo() | |
print(foo:bar(2)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment