Skip to content

interface can extend itself and other possible oddities. #1110

@lenscas

Description

@lenscas

The following code compiles:

local record Bar
    interface Foo
        is Foo
    end
end

https://teal-playground.netlify.app/?c=bG9jYWwgcmVjb3JkIEJhcgogICAgaW50ZXJmYWNlIEZvbwogICAgICAgIGlzIEZvbwogICAgZW5kCmVuZAo%3D

The other oddities, these are either extension of bugs or possibly features so not sure if they should get separate issues or not:
Interface with __call metamethod can be called. (extension of #930 ? Thought there was a similar issue specifically for interfaces but couldn't find it.)

local record Bar
    interface Foo
        metamethod __call: function(_:self)
    end
end

Bar.Foo() --allowed

https://teal-playground.netlify.app/?c=bG9jYWwgcmVjb3JkIEJhcgogICAgaW50ZXJmYWNlIEZvbwogICAgICAgIG1ldGFtZXRob2QgX19jYWxsOiBmdW5jdGlvbihfOnNlbGYpCiAgICBlbmQKZW5kCgpCYXIuRm9vKCk%3D

I personally don't mind this one as much if there was a way to actually safely provide this method to be called. Sadly, right now the only way to do so requires sidestepping teal. Thus losing the benefits of static types.

Types can extend generics.

local record Boo
    interface Foo<T>
        is T
    end
    interface Bar end
end

local function bar(_ : Boo.Bar) end
local a : Boo.Foo<Boo.Bar> = {}

bar(a) --compiles

https://teal-playground.netlify.app/?c=bG9jYWwgcmVjb3JkIEJvbwogICAgaW50ZXJmYWNlIEZvbzxUPgogICAgICAgIGlzIFQKICAgIGVuZAogICAgaW50ZXJmYWNlIEJhciBlbmQKZW5kCgpsb2NhbCBmdW5jdGlvbiBiYXIoXyA6IEJvby5CYXIpIGVuZApsb2NhbCBhIDogQm9vLkZvbzxCb28uQmFyPiA9IHt9CgpiYXIoYSk%3D

I think you can do some cool stuff with this? However if you change Bar from an interface to a Record then it will only fail to compile at the function call, stating that Foo is not a Bar.

https://teal-playground.netlify.app/?c=bG9jYWwgcmVjb3JkIEJvbwogICAgaW50ZXJmYWNlIEZvbzxUPgogICAgICAgIGlzIFQKICAgIGVuZAogICAgcmVjb3JkIEJhciBlbmQKZW5kCgpsb2NhbCBmdW5jdGlvbiBiYXIoXyA6IEJvby5CYXIpIGVuZApsb2NhbCBhIDogQm9vLkZvbzxCb28uQmFyPiA9IHt9CgpiYXIoYSk%3D

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions