Replies: 1 comment 1 reply
-
LuaLS requires annotations to perform better type infer. ---@class Class
local Class = {}
Class.__index = Class
Class.myMethod = function () print("hellow world") end
Class.new = function ()
---@class Class
local o = {}
-- you can define other fields for `o` here
-- which will be auto "injected" into the class `Class` as member fields
o.x = 1
o.y = 2
setmetatable(o, Class)
return o
end
local inst = Class.new() -- server will infer that `Class.new -> Class`
inst:my --> now this will have completion for `myMethod` |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Assume I am trying to use OOP in Lua in the following manner:
It seems that when I input till the colon, the language server can not figure out that inst has a metatable which
__index
is set toClass
, and thus can not prompt completion formyMythod
. Is this impossible to figure out since it is a runtime thing? Do you have any recommended pattern to implement the similar effect, but make the language server know the existence of the methods or member variables?Beta Was this translation helpful? Give feedback.
All reactions