Created
September 7, 2016 19:23
-
-
Save bartzy/90d038d5f33e9c965b691540d23cdda5 to your computer and use it in GitHub Desktop.
Revisions
-
Bar Ziony created this gist
Sep 7, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ /* 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? */ }