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

[Feature request]: Cast class fields

Open lmburns opened this issue 2 years ago • 3 comments

If I have any class, and I get a warning like the following on a function that is being given a class field as a parameter (e.g., tohex(class_name.class_field)):

Cannot assign `string|integer` to parameter `integer`.
[Lua Diagnostics.: param-type-mismatch]

Is there already a way to cast this class field? If not, could it be implemented?

When trying to cast something like ---@cast group.foreground -string, I get the warning:

Unknown type conversion variable `{}`.
[Lua Diagnostics.: unknown-cast-variable]

lmburns avatar Jun 29 '22 18:06 lmburns

I have never thought of a good implementation method for the time being

sumneko avatar Jun 30 '22 07:06 sumneko

Just hit this same issue with a different use case.

---@class MapPosition
---@field x integer
---@field y integer

local targetPos  ---@type MapPosition

---@class Data
local data = {
    targetPosition = nil ---@type MapPosition|nil
}

---@param x MapPosition
local function demandMapPositionClass(x)
end

if data.targetPosition ~= nil then
    targetPos = data.targetPosition -- This has set targetPos to a type of nil or MapPosition. But a nil value can't enter this block.
else
    targetPos = {x = 1, y = 1}
end
demandMapPositionClass(targetPos) -- This warns as targetPos can now contain nil.

I can use an @as on the targetPos = data.targetPosition line to force it to not include the nil type, but ideally a scope limited cast within this block would be better.

muppet9010 avatar Jul 20 '22 17:07 muppet9010

would be nice to have this soon-ish.

FlashHit avatar Jul 21 '22 21:07 FlashHit