Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement