Suggestions for improving our constructors? #3286
Replies: 3 comments 4 replies
-
|
In a perfect world, I feel like passing |
Beta Was this translation helpful? Give feedback.
-
|
While I don't have any good ideas atm (since you said refactoring is not a solution due to tons of legacy code 😇)
You may already knew it, that you can only "inject" fields to variables that have
|
Beta Was this translation helpful? Give feedback.
-
|
I suddenly have an idea that, maybe you can use the plugin feature to achieve your need 🤔
Here's my try
function OnSetText(uri, text)
-- print(uri, text)
-- fix custom constructor
local diffs = {}
for start, overload, className, finish in text:gmatch '()(---@overload[^\n]+\n)local (%w+) = Class%(func[^\n]+\n()' do
-- print(start, "|", overload, "|", className, "|", finish)
diffs[#diffs+1] = {
start = start,
finish = start-1,
text = overload:gsub("fun%(", "fun(self: self, "),
}
diffs[#diffs+1] = {
start = finish,
finish = finish-1,
text = ("---@class %s\n---@diagnostic disable-next-line\nlocal self = {}\n"):format(className),
}
end
return #diffs > 0 and diffs or nil
end
PS: duno why need to use demo( however...I experienced some issues with this "plugin" system...
edited:
edit2I updated the string replace pattern again to
|
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, we are using a class system which requires passing in the constructor as a lambda. This plays quite poorly with LuaLS as far as I can tell. I've made it work, sort of sometimes, but am wondering if anyone has suggestions to make this more succinct.
Here's what I've got:
Okay, kind of awkward. Let's talk about what I've got here.
TestClass("b")selfas just a normal parameter. In order to fix this, I have to redeclare the class in this scope (3b) and then provide some variable to the declaration to attach to (3c). This is highly unfortunate.After doing this, the autocomplete on
test_instis.field_constr,.field_meth, and:Bar()as expected. If I leave out 3b/3c, then.field_constris missing (because it was an illegal injection).So my question is: Can anyone suggest any ways to clean up this mess? Ideally, 3a just works all the time, 3b and 3c are not necessary at all, and in a perfect world, 2a is not even necessary. We have tons of legacy code and so can not change the way our class system works, and are trying to find a compromise that gives us the best type information and the fewest warnings.
Edit: cleaned up unhelpful wordings.
Beta Was this translation helpful? Give feedback.
All reactions