I am trying to wrap my head around how to properly use the Delegate type from within Silver / Fire. After reading the documentation and a few forum threads, I thought I had a basic understanding. But I am receiving four Type mismatch errors when evaluating between a Delegate as a function parameter and casting Callback typealias.
The exact error messages from the compiler are below:
E: Type mismatch, cannot find operator to evaluate “Delegate!” + “Callback” [/Users/lorenalexm/Projects/Square Surge/Logic/Events.swift (84)]
E: Type mismatch, cannot find operator to evaluate “Delegate!” - “Callback” [/Users/lorenalexm/Projects/Square Surge/Logic/Events.swift (91)]
E: Type mismatch, cannot find operator to evaluate “Delegate!” + “Callback” [/Users/lorenalexm/Projects/Square Surge/Logic/Events.swift (117)]
E: Type mismatch, cannot find operator to evaluate “Delegate!” - “Callback” [/Users/lorenalexm/Projects/Square Surge/Logic/Events.swift (124)]
These errors are raised when attempting to add or remove a handler function to the Delegate.
My understanding was the following public typealias Callback = () → () would be evaluated as a Delegate type. This does not seem to be the case? Might someone be able to lend a thought towards what I am missing? Thank you!
I have attached the complete source file below if it would be helpful. This is an attempt to bring a C# utility I use over to Swift.
// MARK: Placeholder, replace with real types
public enum EventType {
    case notSet
}
// MARK: Delegate type aliases
public typealias Callback = () -> ()
public typealias Callback = (arg: T) -> ()
// MARK: Event exceptions
public class HandlerException: Exception {
    /// Constructor to forward message to base Exception class
    /// - parameter message: The message to be thrown with the exception
    init(_ message: String) {
        super.init(message)
    }
}
public class BroadcastException: Exception {
    /// Constructor to forward message to base Exception class
    /// - parameter message: The message to be thrown with the exception
    init(_ message: String) {
        super.init(message)
    }
}
static internal class EventsManager {
    // MARK: Fields
    static var eventTable: [EventType:Delegate] = [:]
    // MARK: Class functions
    /// Adds event KVP to the table if not already present
    /// - parameter type: Key for the event
    /// - parameter handler: Delegate response for the event
    static func onAdding(_ type: EventType, handler: Delegate) {
        if eventTable.keys.contains(type) == false {
            _ = eventTable.updateValue(handler, forKey: type)
        }
        
        var delegate: Delegate = eventTable[type]
        if delegate != nil && delegate.GetType() != handler.GetType() {
            throw HandlerException("Attempting to add handler with inconsistent signatures for event type \(type).")
        }
    }
    
    /// Removes event KVP from table if delegate is no longer assigned
    /// - parameter type: Key for the event
    /// - parameter handler: Delegate response for the event
    static func onRemoved(_ type: EventType, handler: Delegate) {
        var delegate: Delegate? = eventTable[type]
        if delegate == nil {
            _ = eventTable.removeValue(forKey: type)
        }
    }
    
    /// Checks if the KVP exists within the table, throws a BroadcastException if not
    /// - parameter type: Key for the event
    static func onBroadcasting(type: EventType) {
        if eventTable.keys.contains(type) == false {
            throw BroadcastException("Broadcasting event \(type), but no listeners are found.")
        }
    }
    
    /// Creates a new BroadcastException for signature mismatches
    /// - parameter type: The EventType the broadcast was sent with
    static public func signatureExceptionFor(_ type: EventType) -> BroadcastException {
        BroadcastException("Broadcasting event \(type) but listeners have a different signature than the broadcaster.")
    }
}
static public class Events {
    // MARK: Fields
    static private var eventTable: [EventType:Delegate] = EventsManager.eventTable
    //MARK: Class functions
    /// Adds the Callback handler to the event table
    /// - parameter type: Event to which the handler will be added
    /// - parameter handler: Handler to be called when event is received
    public static func addHandler(type: EventType, handler: Callback) {
        EventsManager.onAdding(type, handler: handler)
        eventTable[type] += handler
    }
    
    /// Removes the Callback handler from the event table
    /// - parameter type: Event from which the handler will be removed
    /// - parameter handler: Handler to be called when event is received
    public static func removeHandler(type: EventType, handler: Callback) {
        eventTable[type] -= handler
        EventsManager.onRemoved(type, handler: handler)
    }
    
    /// Broadcasts an event to all handlers
    /// - parameter type: Event to broadcast
    public static func broadcast(type: EventType) {
        let callback: Callback = eventTable[type] as! Callback
        if callback != nil {
            callback()
        } else {
            throw EventsManager.signatureExceptionFor(type)
        }
    }
}
static public class Events {
    // MARK: Fields
    static private var eventTable: [EventType:Delegate] = EventsManager.eventTable
    // MARK: Class functions
    /// Adds the Callback handler to the event table
    /// - parameter type: Event to which the handler will be added
    /// - parameter handler: Handler to be called when event is received
    public static func addHandler(type: EventType, handler: Callback) {
        EventsManager.onAdding(type, handler: handler)
        eventTable[type] += handler
    }
    
    /// Removes the Callback handler from the event table
    /// - parameter type: Event from which the handler will be removed
    /// - parameter handler: Handler to be called when event is received
    public static func removeHandler(type: EventType, handler: Callback) {
        eventTable[type] -= handler
        EventsManager.onRemoved(type, handler: handler)
    }
    
    /// Broadcasts an event to all handlers
    /// - parameter type: Event to broadcast
    /// - parameter argument: Argument to be passed along with the event
    public static func broadcast(type: EventType, argument: T) {
        let callback: Callback = eventTable[type] as! Callback
        if callback != nil {
            callback(argument)
        } else {
            throw EventsManager.signatureExceptionFor(type)
        }
    }
}