Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. public class AnyCup<LiquidType: Liquid>: Cup {
  2.  
  3. // inner mechanism to "remember" the behavior of the cup passed in the init function
  4. private let fillClosure: (LiquidType) -> Void
  5. private let liquidClosure: () -> LiquidType?
  6.  
  7. init<CupType: Cup>(with cup: CupType) where CupType.LiquidType == LiquidType {
  8. self.fillClosure = cup.fill
  9. self.liquidClosure = { return cup.liquid }
  10. }
  11.  
  12. // conformance to Cup protocol
  13. public var liquid: LiquidType? {
  14. return self.liquidClosure()
  15. }
  16.  
  17. public func fill(with liquid: LiquidType) {
  18. self.fillClosure(liquid)
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement