You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
self should be used to refer to the type of the method caller. And the self method should replace the generic method to annotate the type in this use case
Such type annotations should work, and code completion should show self as the caller's type
Reproduction steps
Copy example to editor
Call code prompt
The prompt result is not as expected
Additional Notes
No response
Log File
No response
The text was updated successfully, but these errors were encountered:
I think you misused the backtick capture annotation `T`, this is for capturing a literal string param and convert it into a class. See the example Capture with Backticks in the wiki: https://luals.github.io/wiki/annotations/#generic
In your case you should just use T (without backtick) when capturing the type of self param.
Secondly
your Plane.new({}) call syntax seems to be wrong. Because you declare Vehicle:new() using a : syntax (that's why the 1st argument will be a self), I think you should write Plane:new({}) or Plane:new() instead. Otherwise the empty table {} you passed into it will become self as it's the 1st param when you call the Plane.new() function.
Thirdly
the metatable key is __index but not __index__ (maybe you just mistyped? 😄 ), otherwise the above code snippet will not work as Plane.new will be nil instead.
In short I think your code would be something like this:
---@classVehicleVehicle= {}
---comment---@genericT---@paramselfT---@paramo?T---@returnTfunctionVehicle:new(o)
o=oor {}
setmetatable(o, self)
self.__index=selfreturnoend---@classPlane:VehiclePlane= {}
setmetatable(Plane, { __index=Vehicle })
localobj=Plane:new({}) -- now `obj` will be recognized correctly as `Plane` type by LuaLsprint(getmetatable(obj) ==Plane) -- trueprint(getmetatable(obj) ==Vehicle) -- falselocalobj2=Vehicle:new() -- `obj2` will be recognized as `Vehicle` type by LuaLsprint(getmetatable(obj2) ==Plane) -- falseprint(getmetatable(obj2) ==Vehicle) -- true
How are you using the lua-language-server?
Other
Which OS are you using?
Windows
What is the issue affecting?
Annotations, Type Checking
Expected Behaviour
The code prompt given when calling a constructor annotated with generic parameters that is inherited by a subclass is incorrect.
The prompt message should be
And the type of the
obj
variable in the code hint should bePlane
instead of unknownActual Behaviour
self
should be used to refer to the type of the method caller. And theself
method should replace the generic method to annotate the type in this use caseSuch type annotations should work, and code completion should show
self
as the caller's typeReproduction steps
Additional Notes
No response
Log File
No response
The text was updated successfully, but these errors were encountered: