-
-
Save natecook1000/62c2e49d781515568c83 to your computer and use it in GitHub Desktop.
Swift optional generic parameters?
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
protocol FactoryAType { | |
typealias Product | |
} | |
protocol FactoryBType { | |
typealias Product | |
} | |
struct CombinedFactory<T: FactoryAType, U: FactoryBType where T.Product == U.Product> { | |
let factoryA: T | |
let factoryB: U? | |
init(factoryA: T, factoryB: U? = nil) { | |
self.factoryA = factoryA | |
self.factoryB = factoryB | |
} | |
} | |
struct IntAFactory: FactoryAType { | |
typealias Product = Int | |
} | |
struct IntBFactory: FactoryBType { | |
typealias Product = Int | |
} | |
let fa = IntAFactory() | |
let fb = IntBFactory() | |
let allGood = CombinedFactory(factoryA: fa, factoryB: fb) | |
// I usually write this--it'd be declared this way as part of a type, anyway: | |
let combinedFactory: CombinedFactory<IntAFactory, IntBFactory> = CombinedFactory(factoryA: fa) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment