teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.12k stars 107 forks source link

[next] interface methods #757

Closed Andre-LA closed 1 month ago

Andre-LA commented 3 months ago

Currently is possible to add functions and methods to interfaces, however, as discussed on Matrix:

Functions are meant to be allowed in interfaces, but only as field declarations, not as standalone function entries

Interestingly however, it typechecks as a feature:

local interface IFoo
    t: string
end

function IFoo:print()
end

local record Foo
    is IFoo
end

-- error: type signature of 'print' does not match its declaration in Foo: argument 0: Foo is not a IFoo
-- changing to Foo.print(self: IFOO) works though
function Foo:print() 
    print(self.t)
end

local function bar(v: IFoo)
    -- currently accepted by tl (next-branch) because of `IFoo:print` method
    v:print()
end

local f: Foo = setmetatable({ t = "hey" }, { __index = Foo })

bar(f)
hishamhm commented 1 month ago

Closing this one since next now enforces interfaces to be abstract!