Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork391
Suggestions for improving our constructors?#3286
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: ---@classTestClass---@overloadfun(self:self,param:string):self -- 2a---@overloadfun(param:string):self -- 2blocalTestClass=Class(function(self,param)-- 1---@classTestClass -- 3bself=self-- 3cself.field_constr=param-- 3aend)functionTestClass:Bar()self.field_meth="a"-- 4endlocaltest_inst=TestClass("b") Okay, kind of awkward. Let's talk about what I've got here.
After doing this, the autocomplete on 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. |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 3 comments 7 replies
-
In a perfect world, I feel like passing |
BetaWas this translation helpful?Give feedback.
All reactions
-
Side note to this: What is the actual behaviour of the |
BetaWas this translation helpful?Give feedback.
All reactions
-
I also noticed this There are issues reporting this:
And the workaround to that inheritance issue:#3057 (comment) |
BetaWas this translation helpful?Give feedback.
All reactions
-
Ah interesting good to know. Thank you for your insights as always! 🙏 |
BetaWas this translation helpful?Give feedback.
All reactions
-
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
|
BetaWas this translation helpful?Give feedback.
All reactions
-
That all makes sense, thank you for the clear explanation! I really sympathize with the contributors on this project, lua is super weird and lets you always do weird things, creating reasonable default behavior must be such a challenge! |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I suddenly have an idea that, maybe you can use theplugin feature to achieve your need 🤔
Here's my try
functionOnSetText(uri,text)-- print(uri, text)-- fix custom constructorlocaldiffs= {}forstart,overload,className,finishintext: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), }endreturn#diffs>0anddiffsornilend
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
|
BetaWas this translation helpful?Give feedback.
All reactions
🎉 1
-
Wow this is extremely interesting, thank you! This seems like a pretty powerful feature, I'll have to investigate further! |
BetaWas this translation helpful?Give feedback.
All reactions
-
Okay! This seems to be working, I'm hugely grateful for this! The team is going to be very happy. 🙏 Thank you so much! As an aside though, when trying to explore this, I'm unable to get a |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
(I assume you use vscode)
{"Lua.misc.parameters": ["--develop=true" ],} |
BetaWas this translation helpful?Give feedback.
