Annotation Help: Mutated Classes #3069
-
---@class MyClass
instance = MyClass.new()
instance.a = 4
instance.b = 5
instance:finalize()
---@class MyClassModified
instance... After calling finalize, instance is modified in such a way to make all properties read-only, as well as having new() and finalize() removed. I'd like the editor to see instance as a 'new' class (both MyClass and MyClassModified are fully annotated elsewhere) after it has been finalized. Is it possible using the above semantics?, or do I need to make finalize() return the modified instance as a new class? I'm hoping to avoid: mod_instance = instance:finalize() Hope that makes sense, sorry if this is dumb. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I think use like this: ---@class MyClassData
---@field name string
---@field age number
---@class MyClassFactor : MyClassData
local factor = {}
---@return MyClassData
function factor.new()
end
|
Beta Was this translation helpful? Give feedback.
-
If what you want to avoid is creating another variable (thus avoid variable renaming in the rest of the code), ---@class MyClassModified
---@field a integer
---@field b integer
---@class MyClass: MyClassModified
local MyClass = {}
---@return MyClass
function MyClass.new()
return {}
end
---@return MyClassModified
function MyClass:finalize()
return self
end
---
local instance = MyClass.new()
instance.a = 4
instance.b = 5
local instance = instance:finalize()
instance. -- now only suggests `a`, `b`, without `new` or `finalize` |
Beta Was this translation helpful? Give feedback.
I think what author wants is
@cast
, but seems it is not perfect