lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

`generic` inheritance does not work

Open RomanSpector opened this issue 3 years ago • 1 comments

The second case
Core = {};

---@class Rotation
---@field Enabled boolean
local Rotation = {}

function Rotation:OnLoad() end
function Rotation:OnUnload() end

---@class RotationEngine
Core.RotationEngine = {}

---@generic T
---@param name `T`
---@return T|Rotation rotation
function Core.RotationEngine.AddRotation(name) end

---@class MyRotation
---@field testfield table
local MyRotation = {};

function MyRotation:Test() end

local rotation = Core.RotationEngine.AddRotation("MyRotation");

---@param self MyRotation
---@return boolean
local function myFunc(self) end

local a = myFunc(rotation);

In the first case, in the MyRotation class, we seek methods of the Rotation class,

image

but also an error Cannot assign MyRotation|Rotation to parameter MyRotation.Lua Diagnostics.(param-type-mismatch)

image

The second case
Core = {};

---@class Rotation
---@field Enabled boolean
local Rotation = {}

function Rotation:OnLoad() end
function Rotation:OnUnload() end

---@class RotationEngine
Core.RotationEngine = {}

---@generic T: Rotation
---@param name `T`
---@return T rotation
function Core.RotationEngine.AddRotation(name) end

---@class MyRotation
---@field testfield table
local MyRotation = {};

function MyRotation:Test() end

local rotation = Core.RotationEngine.AddRotation("MyRotation");

---@param self MyRotation
---@return boolean
local function myFunc(self) end

local a = myFunc(rotation);

In the second case there is no inheritance

image

and there is no error either

image

RomanSpector avatar Jul 02 '22 10:07 RomanSpector

Need to add cross type features:

---@generic T
---@param name `T`
---@return T & Rotation rotation
function Core.RotationEngine.AddRotation(name) end

sumneko avatar Jul 02 '22 11:07 sumneko