Code Monkey home page Code Monkey logo

Comments (1)

Benjamin-Dobell avatar Benjamin-Dobell commented on June 28, 2024

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

from intellij-luanalysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.