tl icon indicating copy to clipboard operation
tl copied to clipboard

[next] interface methods

Open Andre-LA opened this issue 8 months ago • 0 comments

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)

Andre-LA avatar Jun 30 '24 01:06 Andre-LA