Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties (getter/setter) support #2874

Open
haruomaki opened this issue Sep 28, 2024 · 1 comment
Open

Properties (getter/setter) support #2874

haruomaki opened this issue Sep 28, 2024 · 1 comment

Comments

@haruomaki
Copy link

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?

@tomlau10
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants