Created
September 7, 2016 19:23
-
-
Save bartzy/90d038d5f33e9c965b691540d23cdda5 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
| /* Binders should adopt Sequence */ | |
| protocol BinderProtocol : Sequence { | |
| associatedtyped Paper | |
| var width : Int { get } | |
| var height : Int { get } | |
| } | |
| // Paper types | |
| struct ThickPaper { | |
| ... | |
| } | |
| struct RecycledPaper { | |
| ... | |
| } | |
| // Adoping Binder types | |
| struct Book: BinderProtocol { | |
| typealias Paper = ThickPaper | |
| var width: Int | |
| var height: Int | |
| private var pages: [ThickPaper] | |
| func makeIterator() -> AnyIterator<ThickPaper> { | |
| return AnyIterator(self.pages.makeIterator()) | |
| } | |
| } | |
| struct EnvironmentalNotebook: BinderProtocol { | |
| typealias Paper = RecycledPaper | |
| var width: Int | |
| var height: Int | |
| private var pages: [RecycledPaper] | |
| func makeIterator() -> AnyIterator<RecycledPaper> { | |
| return AnyIterator(self.pages.makeIterator()) | |
| } | |
| } | |
| /* Enum type to circumvent the fact that the PAT (BinderProtocol) can't be used as a type for variables etc */ | |
| enum Binder { | |
| case book(Book) | |
| case notebook(EnvironmentalNotebook) | |
| var width: Int { | |
| switch self { | |
| case .book(let book): return book.width | |
| case .notebook(let notebook): return notebook.width | |
| } | |
| } | |
| ... | |
| /* How should we adopt Sequence in the Binder enum as well? */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment