Skip to content

Instantly share code, notes, and snippets.

@bartzy
Created September 7, 2016 19:23
Show Gist options
  • Save bartzy/90d038d5f33e9c965b691540d23cdda5 to your computer and use it in GitHub Desktop.
Save bartzy/90d038d5f33e9c965b691540d23cdda5 to your computer and use it in GitHub Desktop.

Revisions

  1. Bar Ziony created this gist Sep 7, 2016.
    57 changes: 57 additions & 0 deletions pats-enum.swift
    Original 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? */
    }