Last active
June 9, 2020 09:00
-
-
Save vedon/cfd04aeaf4bf1ec36e7466d3a5f636d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Swinject | |
public typealias DIContainer = Container | |
public final class AnyInitializer { | |
private let resolve:(_ container: DIContainer) -> Any | |
public init<D1, D2, T>(_ function: @escaping (D1, D2) -> T) { | |
resolve = { container in | |
let d1: D1 = container.resolve(D1.self)! | |
let d2: D2 = container.resolve(D2.self)! | |
return function(d1, d2) | |
} | |
} | |
public init<D1, D2, D3, T>(_ function: @escaping (D1, D2, D3) -> T) { | |
resolve = { container in | |
let d1: D1 = container.resolve(D1.self)! | |
let d2: D2 = container.resolve(D2.self)! | |
let d3: D3 = container.resolve(D3.self)! | |
return function(d1, d2, d3) | |
} | |
} | |
public func resolve<T>(by container: DIContainer) -> T { | |
guard let o = resolve(container) as? T else { | |
fatalError() | |
} | |
return o | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment