Created
May 12, 2019 02:03
-
-
Save twittemb/fefba8b48e860861aaae763cdca1ec8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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