LuaLS/lua-language-server

Properties (getter/setter) support

haruomaki opened this issue · 1 comments

Lua's variables can act as C#-like properties by setting __newindex appropriately.

Getters and setters occasionally have different type signatures. However, the @field annotation can only represent one type.

-- hoge.meta.lua

---@meta

---@class Point
---@field x number
---@field y number
Point = {}

---@class Hoge
---@field position Point ⚠️ actually a property
Hoge = {}

---@return Hoge
function new_hoge() end
-- main.lua

local hoge = new_hoge()
hoge.position = { 3, 7 } -- emits "Missing required fields in type `Point`: `x`, `y`"
print(hoge.position.x)   -- 3.0
print(hoge.position.y)   -- 7.0

(Full sample program is here)

It should be annotated as "position's setter receives a table<number, number>, and its getter returns a Point."

How about adding a new syntax to describe this?

This seems like a duplicated feature request as #1298, which requesting support for setting / getting the same field with different types.