Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Frityet opened this issue Jun 27, 2024 · 3 comments
Closed

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

Frityet opened this issue Jun 27, 2024 · 3 comments
Labels
semantics Unexpected or unsound behaviors

Comments

@Frityet
Copy link

Frityet commented Jun 27, 2024

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
hishamhm added a commit that referenced this issue Aug 19, 2024
@hishamhm hishamhm added the semantics Unexpected or unsound behaviors label Aug 19, 2024
@hishamhm
Copy link
Member

hishamhm commented Aug 19, 2024

@Frityet I added a first-class self type to the language. We already had some heuristics before to detect methods based on the "self" name and type of the first argument. These heuristics have been adapted to detect the self type.

So, in current next, if you change your example above to say either one of:

local interface ISoundMaker
    makesound: function(self: ISoundMaker) -- heuristic: "self" name + definition type => "self" type
end

or

local interface ISoundMaker
    makesound: function(self: self) -- explicit
end

or

local interface ISoundMaker
    makesound: function(self) -- heuristic: "self" name on its own
end

then it will type-check.

@hishamhm
Copy link
Member

Marking this as done! Thanks for reporting and for the test case, which I added to the test suite :)

@Frityet
Copy link
Author

Frityet commented Aug 19, 2024

Marking this as done! Thanks for reporting and for the test case, which I added to the test suite :)

just tested, works like a charm

hishamhm added a commit that referenced this issue Aug 19, 2024
hishamhm added a commit that referenced this issue Aug 21, 2024
hishamhm added a commit that referenced this issue Aug 26, 2024
hishamhm added a commit that referenced this issue Aug 31, 2024
hishamhm added a commit that referenced this issue Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semantics Unexpected or unsound behaviors
Projects
None yet
Development

No branches or pull requests

2 participants