lua-language-server
lua-language-server copied to clipboard
cannot recognize emmy field
I have defined an emmy class role:
---@class role
---@field equipment_mgr equipment_mgr
an emmy class equipment_mgr:
---@class equipment_mgr
local data = {
role = role, ---@type role
equipment = orm_equipment, ---@type table<integer, db.Equipment>
}
then add some method to equipment_mgr:
---@type equipment_mgr
local mt = {}
mt.__index = mt
---@return db.Equipment
function mt:get_equipment(uid)
return self.equipment[uid]
end
Lua Diagnostics cannot recognize the method get_equipment:

while other extension can
You should use:
---@class equipment_mgr
local mt = {}
mt.__index = mt
---@return db.Equipment
function mt:get_equipment(uid)
return self.equipment[uid]
end
@carsakiller It seems that many people will try to declare fields for class by ---@type. Can you add the correct method to the wiki (using ---@class)?
The usage of @field to document class fields is already mentioned in @class and @field, the latter including multiple examples of it as well.
Would it not be documented like this? I believe this is how the wiki explains it.
---@class role
---@field equipment_mgr equipment_mgr
---@class equipment_mgr
---@field role role
---@field equipment db.Equipment[]
local data = {
role = role,
equipment = orm_equipment,
}
---@type equipment_mgr
local mt = {}
mt.__index = mt
---@return db.Equipment
function mt:get_equipment(uid)
return self.equipment[uid]
end
Or would we have to use ---@class equipment_mgr like in your example so that mt:get_equipment is added to the @class type and not just to mt?
@carsakiller It seems that many people will try to declare fields for class by
---@type. Can you add the correct method to the wiki (using---@class)?
If I declare class in this way, then how would I declare data fields in emmy class? It seems declaring fields to class annotated with @type does not workπ€
@sumneko I am confused π. Could you clarify how and why this should be done?
@sumneko I am confused π. Could you clarify how and why this should be done?
incorrect
---@class A
---@type A
local m = {}
m.someValue = 1 -- can not declare field `someValue` into `A`
---@type A
local n
print(n.someValue) -- undefined-field
correct
---@class A
---@class A
local m = {}
m.someValue = 1 -- declare field `someValue` into `A`
---@type A
local n
print(n.someValue) -- integer
just think about this:
---@class A
local m = {}
m.count = 0
---@type A
local n
n.coount = n.coount + 1 -- don't declare `coount` into `A`
@sumneko I have three modules, each module declares some methods to a same class, how could I add these methods into the class?
-- mod1.lua
---@class role
local mt = {}
function mt:method1_1()
end
function mt:method1_2()
end
return mt
-- mod2.lua
---@class role
local mt = {}
function mt:method2_1()
end
function mt:method2_2()
end
return mt
-- mod3.lua
---@class role
local mt = {}
function mt:method3_1()
end
function mt:method3_2()
end
return mt
use @class in all these files will lead to duplicate class definition. Do you have some suggestions? Thank you π
I added a note to @type in the wiki that asks users to instead use @class when trying to add a field. It also links to the above explanation.
@class obviously have to be distinguished from @instance