Class().extends() could return the class

,

Hi, I would have a suggestion about Object API in lua CoreLibs.

for small project, it's OK to put all class in globals, but it's not Pleasant in Developer Experience, a symbol defined by string is put in global namespace magicaly. IDE is confuse and do not know the class symbol.

I dig a bit in CoreLibs object API, and see an undocumented namespaces parameter, basicly a lua table, store it where you want. (I choose to use a namespaces.lua file declaring a global variable namespaces storing all my namespaces, and declaring class slots in the namespaces for IDE)

So my use of object api is

class('Ball', {}, namespaces.fall_in_hole).extends(gfx.sprite)
local Ball <const> = namespaces.fall_in_hole.Ball

It could be great to online this like that :

local Ball <const> = class('Ball', {}, namespaces.fall_in_hole).extends(gfx.sprite)

For that, Only need a small update in CoreLibs object.lua, add one line in extends method so it return the class.

Best regards

2 Likes

I'm also missing this, I hope it will be implemented in the future. As has been stated it is a small addition so it should be easy.

yes this would be handy. I've been doing the following to stop vscode complaining about unknown symbols

MyClass = {}
class("MyClass").extends(MyOtherClass)

the actual class definition overrides the definition of MyClass as a table but it's enough to fool the IDE. Having it returned would be useful tho.

I would also very much like this.

I created my own helper global function to do this, but it'd be nice to not need to

function NewClass(name, parent)
  class(name).extends(parent)
  return _G[name]
end

local S = NewClass("MySprite", playdate.graphcis.sprite)

function S:init() end

You can actually make this change yourself, in your local copy of the library. It's in PlaydateSDK/CoreLibs/object.lua. (Note that you'll have to redo any changes to these files after updating the SDK.) But hopefully we can add this soon.

1 Like