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

Using @return in method messes class "constructor" return type. #798

Closed
MikuAuahDark opened this issue Nov 12, 2021 · 1 comment
Closed
Labels
bug Something isn't working
Milestone

Comments

@MikuAuahDark
Copy link

Describe the bug
When using ---@return to denote method return type in a class, the variable type of resulting class "constructor" gets messed up.

To Reproduce
A reproducible code as follows

local Object = require("classic")

---@class MyClass
local MyClass = Object:extend()

---@return number
function MyClass:myMethod()
	---@type string[]
	self.test = something()
	local len = #self.test
	return len
end

---@type MyClass|fun():MyClass
local MyClass_new
MyClass_new = MyClass

function CreateMyClass()
	local theclass = MyClass_new()
	return theclass
end

Now, what happends here is MyClass_new can be a 2 types as same time. It can act as static/procedural access (MyClass type) or as class constructor (fun():MyClass type). The problem is the variable type of theclass is number|MyClass instead of MyClass. If I remove ---@return number above MyClass:myMethod, the variable type of theclass is MyClass as expected.

As additional bug report, the type of len is NOT a number despite self.test being string[].

Expected behavior

  1. The variable type of theclass is MyClass, not number|MyClass like in v2.3.7.
  2. The type of len variable is number.

Screenshots
gambar

Environment (please complete the following information):

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

Additional context
We use this idiom to define a class:

---@class ClassName
local ClassName = Object:extend() -- we use rxi/classic.lua

-- ... methods

---@type ClassName|fun(param1:any,param2:any,...):ClassName
local ClassName_new
ClassName_new = ClassName
return ClassName_new

Provide logs
file_d%3A_Data_Development_love-11.3-win64_nlay.log

@sumneko sumneko added the bug Something isn't working label Nov 12, 2021
@sumneko sumneko added this to the 2.5.0 milestone Nov 12, 2021
@sumneko
Copy link
Collaborator

sumneko commented Nov 19, 2021

related to #806

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

2 participants