Skip to content

Linter Should Work According to Language Specifications #3035

@jakitliang

Description

@jakitliang

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Diagnostics/Syntax Checking

Expected Behaviour

--- @generic T
--- @param class T
--- @return T
local function New( class )
  return setmetatable({}, {__index = class})
end

local Animal = {}

function Animal:Speak()
  print('Animal:Speak')
end

local Person = New(Animal)

--- Lua Diagnostics: Duplicate field `Speak`
function Person:Speak()
  print('Person:Speak')
end

Person:Speak() -- print Person:Speak

local someone = New(Person)
someone:Speak() -- print Person:Speak

Actual Behaviour

--- @generic T
--- @param class T
--- @return T
local function New( class )
  return setmetatable({}, {__index = class})
end

local Animal = {}

function Animal:Speak()
  print('Animal:Speak')
end

local Person = New(Animal)

--- No Lua Diagnostics
function Person:Speak()
  print('Person:Speak')
end

Person:Speak() -- print Person:Speak

local someone = New(Person)
someone:Speak() -- print Person:Speak

Reproduction steps

  1. Just copy the code and let lsp lint them

Additional Notes

My Points:

  1. Polymorphism is a Core Feature of Object-Oriented Programming:
    Polymorphism is one of the core features of object-oriented programming. It allows different objects to use the same interface but exhibit different behaviors. It is widely used in many programming languages and is a key method for writing maintainable and extensible code.

  2. Lua is a Dynamic Language:
    Lua is dynamically typed, meaning it does not need to determine types at compile time like static languages. This provides great flexibility for programming. Through metatables and mechanisms such as __index and __call, Lua can implement many traditional object-oriented features, like inheritance and polymorphism.

  3. If Polymorphism is Opposed, Static Languages Should Be Chosen Instead of Using duplicate-set-field:
    If dynamic polymorphism is not acceptable, instead of enforcing the duplicate-set-field rule, a feature like @Final annotation should be introduced. Until such a feature is implemented, no changes should be made.
    If dynamic polymorphism is unacceptable, instead of adding duplicate-set-field, developers should consider other programming languages such as C#, Java, or C++, which are statically bound and support the final syntax for methods.

  4. Performance Trade-offs in Scripting Languages:
    The design of scripting languages typically makes trade-offs between performance and flexibility. For example, Python's OOP is based on the dict mechanism, which allows dynamic modification of object behavior. Ruby, on the other hand, uses the Object, BasicObject, Class, and ancestor lookup chains to achieve flexibility, which sacrifices some performance.
    For most scripting languages, performance is not the sole design consideration. Development efficiency, maintainability, and extensibility are the key goals. Over-optimization for performance, especially by limiting flexibility, is contrary to the design intent of scripting languages.

Log File

No response

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