Skip to content

Properties (getter/setter) support #2874

Open

Description

@haruomaki

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?

Activity

  1. tomlau10 commented on Sep 28, 2024

    @tomlau10
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions