Skip to content

Instantly share code, notes, and snippets.

@svermeulen
Created April 14, 2022 00:34
Show Gist options
  • Save svermeulen/2ebe995ad7e398dc7c707a1be0c87674 to your computer and use it in GitHub Desktop.
Save svermeulen/2ebe995ad7e398dc7c707a1be0c87674 to your computer and use it in GitHub Desktop.
Lua Teal Interface Example
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