2017-09-25 Swift example of a factory and a closure typealias

I couldn't find a nice, compact example of a closure typealias in Swift, so here is one you can paste straight into a Playground:

    import Foundation
    typealias MakeClosure = (_ a: Int, _ b: Int) -> Int
    class IntFactory {
        static let instance = IntFactory()
        var makeClosure: MakeClosure?
        func makeInt(a: Int, b: Int) -> Int {
            guard let closure = self.makeClosure else {
                fatalError()
            }
            let result = closure(a, b)
            return result
        }
        private init() {
        }
    }
    func sum(a: Int, b: Int) -> Int {
        let result = a + b
        return result
    }
    IntFactory.instance.makeClosure = sum
    IntFactory.instance.makeInt(a: 22, b: 20)
    // This also works
    let sum2: MakeClosure = { (a: Int, b: Int) in
        a + b
    }
    IntFactory.instance.makeClosure = sum2
    IntFactory.instance.makeInt(a: 22, b: 20)