Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save natecook1000/62c2e49d781515568c83 to your computer and use it in GitHub Desktop.
Save natecook1000/62c2e49d781515568c83 to your computer and use it in GitHub Desktop.
Swift optional generic parameters?
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