Skip to content

Operator overloading is not detected properly #1338

Closed
@kenzocarneiro

Description

@kenzocarneiro

Describe the bug
When creating a class and overloading its operators, the extension doesn't seem to detect the new return value of the operation. The result is detected as a "number":

--- Class representing Vectors.
--- @class Vector
--- @field x number
--- @field y number
Vector = {}

--- Constructor of Vector.
--- @param x number
--- @param y number
--- @return Vector
function Vector:new(x, y)
    local e = {}
    setmetatable(e, self)
    self.__index = self
    e.x = x or 0
    e.y = y or 0
    return e
end

--- Add to another Vector.
--- @param v Vector
--- @return Vector
function Vector:__add(v)
    return Vector:new(self.x + v.x, self.y + v.y)
end

local v = Vector:new(3, 4)
v = v + Vector:new(1, 2) --- Warning here
print(v)

The __add function is detected:
image

But there is still a warning:
image

This may be related to #599, but I'm not sure.

To Reproduce
Create a class and overload the operators. (See the code above).

Expected behavior
This shouldn't trigger a warning: since __add is documented, the result of the operation should be detected as a Vector (or at least an "any" for a sum/div/... with tables).

Environment:

  • OS: Windows 10 x64
  • Is WSL remote? No
  • Client: VSCode

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions