Useful snippet for subscribing to events with additional parameter (Method extension)

Usage example:

using { Utils }
#---
Button.InteractedWithEvent.Subscribe(Handler, 10)
#---
Handler<private>(Agent : agent, Parameter : int) : void =
    Print("Parameter: {Parameter}")

Put this code into separate file:

Utils := module:
    event_handler(T : type, K : type) := class:
        Callback : type{_(:T, :K):void}
        Parameter : K
    
        OnFired<public>(Data : T) : void=
            Callback(Data, Parameter)

    (Event : subscribable(t)).Subscribe<public>(C : type{_(:t, :k):void}, P : k where k : type, t : type) : void =
        Event.Subscribe(event_handler(t, k){Callback := C, Parameter := P}.OnFired)
4 Likes