-
I'm writing docgen for a game's scripting API that implements an event system passing a string as the event name and a callback function with event specific signatures Additionally, classes that implement the event subscribe method inherit events from their base classes (if they have any) I tried the annotations below however calling ---@class A
local a = {}
---Subscribe to an event
---@param eventName string
---@param callback function
---@overload fun(self: A, eventName: "AEvent", callback: fun())
function a:Subscribe(eventName, callback) end
---@class B : A
local b = {}
---Subscribe to an event
---@param eventName string
---@param callback function
---@overload fun(self: B, eventName: "BEvent", callback: fun(arg: string))
function b:Subscribe(eventName, callback) end Is there any way to overload a base class function without overwriting it? (that also works with multiple inheritance) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
This is an interesting use case for classes and inheritance.... I am confused with how this is all fits together. If If you do need both function b:Subscribe(event, callback)
if event == "AEvent" then
return a:Subscribe(event, callback)
end
--- do b:Subscribe stuff...
end |
Beta Was this translation helpful? Give feedback.
-
It is not supported, because in most cases, we want the sub-class cover the same name field in parent-class. |
Beta Was this translation helpful? Give feedback.
-
When in doubt, use ---@alias BaseActorEvents
---| '"Destroy"'
---| '"Spawn"'
---| '"ValueChange"'
---@class BaseActor
local BaseActor = {}
---Subscribe to an event
---@param eventName BaseActorEvents
---@param callback fun()
function BaseActor:Subscribe(eventName, callback) end
---@alias StaticMeshEvents
---| '"TakeDamage"'
---@class StaticMesh : BaseActor
local StaticMesh = {}
---Subscribe to an event
---@param eventName BaseActorEvents | StaticMeshEvents
---@param callback fun(arg: string)
function StaticMesh:Subscribe(eventName, callback) end So long as the functionality of |
Beta Was this translation helpful? Give feedback.
It is not supported, because in most cases, we want the sub-class cover the same name field in parent-class.
Maybe we can only handle your needs by adding a special annotation for the field.