SHARE
TWEET

Untitled

a guest May 22nd, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top