Skip to content

Instantly share code, notes, and snippets.

@svermeulen
Created April 14, 2022 00:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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