Skip to content

Instantly share code, notes, and snippets.

@twittemb
Created May 12, 2019 02:03
Show Gist options
  • Select an option

  • Save twittemb/fefba8b48e860861aaae763cdca1ec8c to your computer and use it in GitHub Desktop.

Select an option

Save twittemb/fefba8b48e860861aaae763cdca1ec8c to your computer and use it in GitHub Desktop.
public class AnyCup<LiquidType: Liquid>: Cup {
// inner mechanism to "remember" the behavior of the cup passed in the init function
private let fillClosure: (LiquidType) -> Void
private let liquidClosure: () -> LiquidType?
init<CupType: Cup>(with cup: CupType) where CupType.LiquidType == LiquidType {
self.fillClosure = cup.fill
self.liquidClosure = { return cup.liquid }
}
// conformance to Cup protocol
public var liquid: LiquidType? {
return self.liquidClosure()
}
public func fill(with liquid: LiquidType) {
self.fillClosure(liquid)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment