For my specific use case, i have some callable objects that acts as events that the application will dispatch (call) in specific situations, something like configure, setup, dispose, etc. I need to perform different actions based on the types of the event being fired. For example, for setup, i do some initialization and dispose cleaning up things. I also wanna group these events with specific types. For example, configure, setup and dispose should be subtype of LifeCycleEvent, and mouse events such as mousepress and mousedrag should be subtype of MouseEvent, and the same for the keyboard events and etc.
Of course, the solution is to define singleton functors:
abstract type Event <: Function end
abstract type LifeCycleEvent <: Event end
abstract type MouseEvent <: Event end
abstract type KeyboardEvent <: Event end
struct SetupEvent <: LifeCycleEvent end
struct ConfigureEvent <: LifeCycleEvent end
struct DisposeEvent <: LifeCycleEvent end
struct MousePressEvent <: MouseEvent end
struct MouseDragEvent <: MouseDrag end
struct KeyPressEvent <: KeyboardEvent end
struct KeyInputEvent <: KeyboardEvent end
const setup = SetupEvent()
const configure = ConfigureEvent()
const dispose = DisposeEvent()
Then, implementing each event:
function (::SetupEvent)()
@info "Starting..."
end
function (::DisposeEvent)()
@info "Closing..."
end
# ... etc
With this, i can create a function called dispatch that acts different based on the event
that is being fired:
function dispatch(event::Event, args...)
if applicable(event, args...)
# fire the event
event(args...)
end
end
function dispatch(event::SetupEvent, args...)
# do something before the app starts
end
function dispatch(event::DisposeEvent, args...)
# do something before the app closes
end
I just think that the syntax (::MyType)() = ... or function (::MyType)() end is a little bit unintuitive, specially for people still learning julia. Instead, if i could just write:
function myfunction <: MyType end
And then defining the methods later could be more easier to understand.
But of course, that is specific to my use case. I don’t know how many people would actually use this.