Strategy to deal with middleclass statics? #2365
Unanswered
jtackaberry
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Nah. Looks lot of works. I have a better approach. Check out the annotation definition I often use: ---@meta
---kikito middleclass instance definition
---@class instance<string, any>
---
---@field class class
---
---@field initialize fun(self: self, ...)
---@field isInstanceOf fun(self: self, other: class|table): boolean
---kikito middleclass class definition
---@class class: instance
---@overload fun(): instance
---
---@field name string
---@field super class?
---@field static self
---
---@field protected __instanceDict instance
---@field protected __declaredMethods table<string, function>
---@field subclasses { [self]: true }
---@field allocate fun(self: self): instance
---@field new fun(self: self): instance
---@field subclass fun(self: self, newSublassName: string): aNewSubclass: self
---@field subclassed fun(self: self, other: self|table)
---@field isSubclassOf fun(self: self, other: self|table): boolean
---@field include fun(self: self, ...): self Then overload the context of loaded middleclass package, on required. Usually once. ---@module 'middleclass'
---@overload fun(className: string, super?: table): class
local middleclass = require('src.libraries.middleclass.middleclass')
---Aliasing 'middleclass.middleclass' into 'middleclass', incase it is required by other libraries.
if package.loaded['middleclass'] ~= middleclass then
package.loaded['middleclass'] = middleclass
end Let's say, I have to define a Superclass for all my project's classes named local weakKeys, weakValues = { __mode = 'k' }, { __mode = 'v' }
local NOOP = function() end
local _initializedInstances = setmetatable({}, weakKeys) ---@type { [Object]: true? }
---Lua reimplementation of https://love2d.org/wiki/Object
---@class ObjectClass: Object, class
---@overload fun(): Object
---@field new fun(): Object
---@field static self
local ObjectClass = require 'middleclass' ('game.Object')
---Define class's statics and also redefine the class itself.
---@class ObjectClass
local staticObjectClass = ObjectClass.static
staticObjectClass._VERSION = '0.0.1-TRY-0'
---@param logger? function|true
---@return table
function staticObjectClass:debugInfoGetAllInstance(logger)
logger = logger == true and print
or type(logger) == 'function' and logger
or NOOP
local t = setmetatable({}, weakValues) -- return a weaktable
for o in pairs(_initializedInstances) do
table.insert(t, o)
logger(#t, o)
end
return t
end
---@return boolean success # `true` if the object was initialized by this call, `false` if it had been previously initialized (prevent from being re-initialized).
function ObjectClass:initialize()
if _initializedInstances[self] then return false end
--> BEGIN
-- some initialization goes here
--> END
_initializedInstances[self] = true
return true
end
---@class Object: instance
local Object = {}
---@return boolean success # `true` if the object was released by this call, `false` if it had been previously released.
function Object:release()
if not _initializedInstances[self] then return true end
_initializedInstances[self] = nil
return false
end
function Object:type()
return self.class and self.class.name or type(self)
end
function Object:typeOf(name)
-- TODO: this checkout class name of current object TIL last super.
end
-- include last
ObjectClass:include(Object)
return ObjectClass From code above, take a look at these line: ---@class ObjectClass
local staticObject = ObjectClass.static
---@param logger? function|true
---@return table
function staticObject:debugInfoGetAllInstance(logger)
-- ...omitted...
end That is how I define a class static. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
#1172 offers an approach to work with middleclass's class initialization:
One of middleclass's features is to set a static values under a static table, after which point those values are accessible from the class:
Unfortunately #1172 didn't address that aspect of the user's question, presumably because handling typing here is problematic.
I can get a bit further by declaring the
static
field on the class via@field
:The only way I can see is to predefine all static values as
@field
below the@class
:This suppresses the undefined field warning and the type of
Foo.bar
is now known, but it's not ideal: when jumping to the definition ofFoo.bar
it of course lands on the@field
line rather than theFoo.static.bar
assignment.Is there a better way to approach this?
Beta Was this translation helpful? Give feedback.
All reactions