tl icon indicating copy to clipboard operation
tl copied to clipboard

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

Open Frityet opened this issue 1 year ago • 0 comments


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

Frityet avatar Jun 27 '24 20:06 Frityet