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

Overrides are checked for static functions #138

Closed
Martmists-GH opened this issue Oct 20, 2022 · 1 comment
Closed

Overrides are checked for static functions #138

Martmists-GH opened this issue Oct 20, 2022 · 1 comment

Comments

@Martmists-GH
Copy link

Screenshot_20221020_155625

@Benjamin-Dobell
Copy link
Owner

Benjamin-Dobell commented Jan 16, 2023

This is intended since your implementation of button is not a covariant of widget, the error reported is accurate. However, you can achieve what you're after with types like:

---@class widget
---@field common string

---@class button : widget
---@field text string
---@field callback fun(btn: button, x: number, y: number)

---@class static__widget
local widget = {}

function widget.create()
    return --[[---@type widget]] {
        common = "some common field"
    }
end

---@class static__button
---@field create fun(text: string, ): button
local button = {}

---@param text string
---@param callback (fun(btn: button, x: number, y: number))
function button.create(text, callback)
    local button = --[[---@type button]] widget.create()
    setmetatable(button, { __index = button })
    button.text = text
    button.callback = callback
    return button
end

Basically your static class can be given its own type, that does not incorrectly indicate it inherits from the static widget type. Your button however does still correctly inherit from widget.

This is not just "correct", it's beneficial, you won't even see the create function on instances of button/widget, which is important, because they're not compatible.

CleanShot 2023-01-16 at 14 48 50

CleanShot 2023-01-16 at 14 49 01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants