teal-language / tl

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

[next] `self` cannot be constrained by an interface #756

Open Frityet opened 1 week ago

Frityet commented 1 week ago

local interface ISoundMaker
    makesound: function(ISoundMaker)
end

local record Animal is ISoundMaker
    species: string
end

function Animal:create(species: string): Animal
    return setmetatable({ species = species }, { __index = Animal })
end

function Animal:makesound()
    print("Animal sound")
end

local record Person is ISoundMaker
    name: string
end

function Person:create(name: string): Person
    return setmetatable({ name = name }, { __index = Person })
end

function Person:makesound()
    print("Person sound")
end

local things: {ISoundMaker} = {
    Animal:create("Dog"),
    Person:create("John")
}

for _, thing in ipairs(things) do
    thing:makesound()
end

Fails with:

========================================
2 errors:
test.tl:14:1: type signature of 'makesound' does not match its declaration in Animal: argument 0: Animal is not a ISoundMaker
test.tl:26:1: type signature of 'makesound' does not match its declaration in Person: argument 0: Person is not a ISoundMaker
----------------------------------------
2 errors