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

The class is not defined if the table is called as a function #1480

Closed
RomanSpector opened this issue Aug 19, 2022 · 3 comments
Closed

The class is not defined if the table is called as a function #1480

RomanSpector opened this issue Aug 19, 2022 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@RomanSpector
Copy link

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?

Type Checking

Expected Behaviour

image

Actual Behaviour

image

Reproduction steps

---@class flag96
flag96 =
{
    part = { 0, 0, 0 };
}
setmetatable(flag96, flag96);

---@param p1? integer
---@param p2? integer
---@param p3? integer
---@return flag96
function flag96:__call( p1, p2, p3)
    local part = {};
    part[1] = p1 or 0;
    part[2] = p2 or 0;
    part[3] = p3 or 0;
    return setmetatable(part, { __index = self });
end

local flags = flag96(1, 2, 3);

Additional Notes

No response

Log File

No response

@sumneko
Copy link
Collaborator

sumneko commented Aug 20, 2022

Use:

---@class flag96
---@overload fun(p1?: integer, p2?: integer, p3?: integer):flag96

@sumneko sumneko added the question User has a question label Aug 20, 2022
@RomanSpector RomanSpector reopened this Sep 5, 2022
@RomanSpector
Copy link
Author

image

---@class AAA
---@field value boolean
---@overload fun(): boolean
local AAA = {};

---@class BBB
local BBB = {};

---@return BBB
function BBB:new() end

---@return AAA
function BBB:add() end

local bbb = BBB:new();
local aaa = bbb:add();

local result = aaa();

@carsakiller
Copy link
Collaborator

carsakiller commented Sep 13, 2022

I have an even more odd workaround:

---@class AAA
---@field value boolean
---@operator call:boolean
local AAA = {};

---@class BBB
local BBB = {};

---@return BBB
function BBB:new() end

---@return AAA
function BBB:add() end

local bbb = BBB:new();
local aaa = bbb:add();

local result = aaa();

I set the __call metamethod to return a boolean by using @operator. It seems that @operator requires at least one parameter so I just set it to _ Turns out you don't need the () at all 🥴

@sumneko sumneko added enhancement New feature or request and removed question User has a question labels Sep 19, 2022
@sumneko sumneko added this to the 3.6.0 milestone Sep 19, 2022
@sumneko sumneko added bug Something isn't working and removed enhancement New feature or request labels Nov 3, 2022
@sumneko sumneko closed this as completed in b063a86 Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants