Evert
Evert

Reputation: 2182

How can I make a class property array be a heterogeneous array of a generic type?

Here's what I want. I'm writing a very simple event dispatcher (click that link to see my code). It was working fine when I only had the listen() and the fire() method. This is how you could use it:

struct UserHasBirthday: Event {
    let name: String

    init(name: String) {
        self.name = name
    }
}

let events = TestDispatcher()
events.listen {
    (event: UserHasBirthday) in
    print("Happy birthday \(event.name)!")
}
events.fire( UserHasBirthday(name: "John Doe") )

That's all well and good, but now I wanted to add the feature that you could push events to a queue and then later fire them all at once. That's why I added the push and flush methods.

Now the problem is that in the flush() method I need to be able to downcast the generic Event type to the specific event type that was given. Otherwise the fire() method doesn't work.

So I thought, maybe I could save the type information in the same array as the event itself. As you can see I tried to do that with a tuple. Unfortunately it doesn't work like that.

I think that if I could find a way to make the variable pushedEvents accept a generic type like so: var pushedEvents = Array<E: Event>() then it could work. But the only way I know to do that is to assign that generic to the whole class like so: class TestDispatcher<E: Event> { }, but then every instance of that class can only be used for one specific type of event and I definitely don't want that.

Does anybody know some kind of way to make this work?

Upvotes: 1

Views: 341

Answers (2)

dfrib
dfrib

Reputation: 73186

The problem is that Swift doesn't allow type conversion to metatypes.

One workaround is to include all types that conform to Event (at least those you will use in your Dispatcher) in a switch case in the flush() function of your TestDispatcher class. It isn't as versatile as the functionality I believe you're looking for, and as you've shown with your own answer, type erasure is the way to here. I'll leave my original answer intact however, as it explains why your original approach of attempting to cast to metatypes didn't work.

public protocol Event {}

public enum Listener<E: Event> {
    public typealias T = E -> ()
}

public protocol Dispatcher {
    func listen<E: Event>(listener: Listener<E>.T)
    func fire<E: Event>(event: E)
    func push<E: Event>(event: E)
    func flush()
}

//

public class TestDispatcher: Dispatcher {
    var listeners = [String:[Any]]()
    var pushedEvents = [Event]()

    public init() {}

    public func listen<E: Event>(listener: Listener<E>.T) {
        var listeners = self.listeners[String(E.self)] ?? []
        listeners += [listener] as [Any]
        self.listeners[String(E.self)] = listeners
    }

    public func fire<E: Event>(event: E) {
        listeners[String(E.self)]?.forEach {
            let f = $0 as! Listener<E>.T
            f(event)
        }
    }

    public func push<E: Event>(event: E) {
        pushedEvents = pushedEvents + [event]
    }

    /* Include a switch case over all types conforming to Event ... */
    public func flush() {
        for event in pushedEvents {
            switch event {
            case let ev as UserHasBirthday: fire(ev)
            case let ev as UserWonTheLottery: fire(ev)
            case _: print("Unknown event type.")
            } 
        }
    }
}

Example usage:

struct UserHasBirthday: Event {
    let name: String

    init(name: String) {
        self.name = name
    }
}

struct UserWonTheLottery: Event {
    let name: String
    let amount: Int

    init(name: String, amount: Int) {
        self.name = name
        self.amount = amount
    }
}

let events = TestDispatcher()
events.listen {
    (event: UserHasBirthday) in
    print("Happy birthday \(event.name)!")
}
events.listen {
    (event: UserWonTheLottery) in
    print("Congratulations \(event.name) for winning \(event.amount)!")
}

events.push(UserHasBirthday(name: "John Doe"))
events.push(UserHasBirthday(name: "Jane Doe"))
events.push(UserWonTheLottery(name: "Jane Doe", amount: 42000))

events.flush()
/* Happy birthday John Doe!
   Happy birthday Jane Doe!
   Congratulations Jane Doe for winning 42000! */

Upvotes: 1

Evert
Evert

Reputation: 2182

This guy on reddit gave me the solution by using a so-called type-erasure pattern (I didn't know about that pattern).

I edited his code to meet my needs more and this is what I have now:

public protocol Event {}

public protocol ErasedListener {
    func matches(eventType: Event.Type) -> Bool
    func dispatchIfMatches(event: Event)
}

public struct Listener<T: Event>: ErasedListener {
    let dispatch: T -> Void

    public func matches(eventType: Event.Type) -> Bool {
        return matches(String(eventType))
    }

    func matches(eventType: String) -> Bool {
        return eventType == String(T.self)
    }

    public func dispatchIfMatches(event: Event) {
        if matches(String(event.dynamicType)) {
            dispatch(event as! T)
        }
    }
}

public protocol Dispatcher {
    func listen<E: Event>(listener: E -> Void)
    func fire(event: Event)
    func queue<E: Event>(event: E)
    func flushQueueOf<E: Event>(eventType: E.Type)
    func flushQueue()
    func forgetListenersFor<E: Event>(event: E.Type)
    func emptyQueueOf<E: Event>(eventType: E.Type)
    func emptyQueue()
}

public class MyDispatcher: Dispatcher {
    var listeners = [ErasedListener]()
    var queuedEvents = [Event]()

    public init() {}

    public func listen<E: Event>(listener: E -> Void) {
        let concreteListener = Listener(dispatch: listener)

        listeners.append(concreteListener as ErasedListener)
    }

    public func fire(event: Event) {
        for listener in listeners {
            listener.dispatchIfMatches(event)
        }
    }

    public func queue<E: Event>(event: E) {
        queuedEvents.append(event)
    }

    public func flushQueue() {
        for event in queuedEvents {
            fire(event)
        }
        emptyQueue()
    }

    public func emptyQueue() {
        queuedEvents = []
    }

    public func flushQueueOf<E: Event>(eventType: E.Type) {
        for event in queuedEvents where String(event.dynamicType) == String(eventType) {
            fire(event)
        }
        emptyQueueOf(eventType)
    }

    public func forgetListenersFor<E: Event>(eventType: E.Type) {
        listeners = listeners.filter { !$0.matches(eventType) }
    }

    public func emptyQueueOf<E: Event>(eventType: E.Type) {
        queuedEvents = queuedEvents.filter { String($0.dynamicType) != String(eventType) }
    }
}

Example usage

struct UserDied: Event {
    var name: String
}

class UserWasBorn: Event {
    let year: Int

    init(year: Int) {
        self.year = year
    }
}

// you can use both classes and structs as events as you can see

let daveDied = UserDied(name: "Dave")
let bartWasBorn = UserWasBorn(year: 2000)

var events = MyDispatcher()

events.listen {
    (event: UserDied) in

    print(event.name)
}

events.listen {
    (event: UserWasBorn) in

    print(event.year)
}

events.queue(daveDied)
events.queue(UserWasBorn(year: 1990))
events.queue(UserWasBorn(year: 2013))
events.queue(UserDied(name: "Evert"))

// nothing is fired yet, do whatever you need to do first

events.flushQueue()
/* 
    This prints:
    Dave
    1990
    2013
    Evert
*/

// You could also have flushed just one type of event, like so:
events.flushQueueOf(UserDied)
// This would've printed Dave and Evert,
// but not the year-numbers of the other events

Upvotes: 1

Related Questions