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

Enhancements to properties

Open vinipsmaker opened this issue 2 years ago • 9 comments
trafficstars

I'd like to declare @fields that might be:

  • ready-only (i.e. __index() exists, but __newindex() doesn't)
  • read-write
  • dynamic (a new value is generated every time __index() is called)

vinipsmaker avatar Feb 11 '23 14:02 vinipsmaker

I can support readonly first.

sumneko avatar Feb 13 '23 06:02 sumneko

I can support readonly first.

What syntax do you have in mind?

vinipsmaker avatar Feb 13 '23 11:02 vinipsmaker

Just add keyword readonly

---@class A
---@field readonly x number
---@field private readonly y number
local A = {}

---@readonly
---@protected
A.z = 1

sumneko avatar Feb 13 '23 11:02 sumneko

I would really like to document dynamic fields, like said above, field generated using the __index function. This would be really helpful when you have a class which has specific fields (so it's not just a table) but also an __index implementation. For example like this:

---@class Scene
---@dynamicField SceneObject? The object with the given name, if present
local scene = { }

--- Finds an object by name
function scene.find(name) end

--- FInds an object by id
function scene.findById(id) end

setmetatable(scene, { __index = function() end }) -- You know what I mean

-- Example usage
local player = scene.player

Currently this would cause a warning because player isn't a field of Scene.

You wouldn't need an option to set the name of the field with the @dynamicField (or @dynamic I guess), because that depends on the actual field requested. For the documentation, if you want to go all out you could add something like @name to be inserted into the documentation comment, which would then be replaced by the actual name of the field you access when you hover over it, but I guess that's not so important.

Rc-Cookie avatar Feb 22 '23 12:02 Rc-Cookie

Just to document my own use case for dynamic fields, I'm worried about an user caching values:

local pos = obj.getPos()

-- much later...

pos.x --< no longer reflects `obj`'s state

vinipsmaker avatar Feb 22 '23 14:02 vinipsmaker

Just to document my own use case for dynamic fields, I'm worried about an user caching values:

local pos = obj.getPos()

-- much later...

pos.x --< no longer reflects `obj`'s state

Is this supposed to be a comment on my post, or unrelated?

Rc-Cookie avatar Feb 22 '23 19:02 Rc-Cookie

Is this supposed to be a comment on my post, or unrelated?

Unrelated. I just wanted to make sure that everybody understands that multiple use cases are on the table.

vinipsmaker avatar Feb 24 '23 00:02 vinipsmaker

The request in this issue clashes with the request in issue #1298.

C3pa avatar Mar 27 '23 09:03 C3pa

The request in this issue clashes with the request in issue #1298.

if we had a never type (the type with no values) then this one becomes a subset of that one - a readonly field is one whos write type is never, and writeonly one whos read type is never - and never would be a sensible default for the unspecified one if a field is only specified on one side!

justarandomgeek avatar Sep 24 '24 15:09 justarandomgeek