Closed notpeter closed 1 year ago
Also need to somehow inject .super
as a field for new classes because we need to run self.super.init()
in our MyClass:init()
.
Workaround:
---@field super any
@notpeter
---@class TextSprite: playdate.graphics.sprite
---@field text string
---@field font _Font
---@field alignment integer
---@overload fun(text: string): TextSprite
TextSprite = class("TextSprite").extends(playdate.graphics.sprite) or TextSprite -- no-op for LuaLS
---init
---@param text string
function TextSprite:init(text)
self.text = text
end
-- t Will be recognized as TextSprite
local t = TextSprite("str")
Thanks for this @erasin. I had tried to use ---@field __call
but it didn't work.
I updated the readme to include this.
I wish there was a way to explicitly document the constructor with comments and just not the limited fun()
function format. Perhaps via ---@meta
if you included both the ---@class
and an identically named function stub with LuaCATS comments. Dunno.
The Playdate Lua SDK includes an object/class implementation which is called with the following syntax:
Both these parts are required, the bulk of the class setup actually occurs in
.extends
andclass("MySprite")
alone does not create a usable class. In the example above there is an implicit creation of a MySprite in the_G
table making it globally accessible -- this makes it difficult for lua-language-server to know where aMySprite
object is declared.Currently I am using the following workaround:
I have submitted an enhancement request at devforum.play.date for a trivial enhancement which have
.extends()
returning the class (it currently returns nil). This would allow the following syntax which feels relatively clear, if still technically redundant:I think with this I would not define the return type of
.extends()
which would let the type-checker off the hook.