You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I stumbled in this fix by accident and I believe this can fundamentally enhance lua experience with zero changes to either's codebase save for the addition of a macro in luacats called @constructor that expands into this.
The core problem - Lua OOP is widely used, the colon operator is efficient and not archaic in any way nor an antipattern - if anything it is totally acceptable. However the catch is that colon syntax becomes useless when you introduce the class-instance differentiation leading to red and yellow squiggles about undefined fields and type errors. That automatically breaks autocompletion on self-object too. The usual fix is to annotate a return value of a particular instance with a specific shape but this is cumbersome and can lead to massive boilerplate code on top of luacats namespace pollution.
This is the fix -
---@class X : X.class : instance
---@class Y : Y.class : instance
---@class X.class : class
---@field a number
---@field b number
---@overload fun(...): X
local X = class 'X'
X.a = 1
X.b = 2
function X:print()
end
---@class Y.class : X.class
---@field c number
---@overload fun(...): Y
local Y = class('Y', X)
local y = Y()
y:print()
This works out of the box and supports autocompletion across all the inherited classes. Secondly the solution can be custom built for all sorts of OOP libraries because all the user has to do is define the shape of the object using --@class and then inherit from whatever class/instance shape they have defined. With this you can have python-style isinstance and issubclass functionality that properly distinguishes between classes and instances solving a wide varity of headaches
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
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 stumbled in this fix by accident and I believe this can fundamentally enhance lua experience with zero changes to either's codebase save for the addition of a macro in luacats called
@constructorthat expands into this.The core problem - Lua OOP is widely used, the colon operator is efficient and not archaic in any way nor an antipattern - if anything it is totally acceptable. However the catch is that colon syntax becomes useless when you introduce the class-instance differentiation leading to red and yellow squiggles about undefined fields and type errors. That automatically breaks autocompletion on self-object too. The usual fix is to annotate a return value of a particular instance with a specific shape but this is cumbersome and can lead to massive boilerplate code on top of luacats namespace pollution.
This is the fix -
This works out of the box and supports autocompletion across all the inherited classes. Secondly the solution can be custom built for all sorts of OOP libraries because all the user has to do is define the shape of the object using
--@classand then inherit from whatever class/instance shape they have defined. With this you can have python-style isinstance and issubclass functionality that properly distinguishes between classes and instances solving a wide varity of headachesAll reactions