$30 off During Our Annual Pro Sale. View Details »

enum as Option.

r-plus
June 21, 2018

enum as Option.

enum as Option.
potatotips #52

r-plus

June 21, 2018
Tweet

More Decks by r-plus

Other Decks in Technology

Transcript

  1. FOVN
    BT0QUJPO
    6/21 potatotips #52
    taiki komaba(@r_plus)

    View Slide

  2. UBJLJLPNBCB
    !S@QMVT

    View Slide

  3. 0QUJPO4FU

    View Slide

  4. CJUຖʹΦϓγϣϯΛׂ౰ͯΔ
    ΦϓγϣϯͷͨΊͷ΋ͷͬΆ͍

    View Slide

  5. public extension DispatchQueue {
    public struct Attributes : OptionSet {
    public let rawValue: UInt64
    public init(rawValue: UInt64) { self.rawValue = rawValue }
    public static let concurrent = Attributes(rawValue: 1<<1)
    @available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
    public static let initiallyInactive = Attributes(rawValue: 1<<2)

    'PVOEBUJPOͰ΋
    Α͘࢖ΘΕ͍ͯΔ
    https://github.com/apple/swift/blob/master/stdlib/public/SDK/Dispatch/Queue.swift

    View Slide

  6. struct ShippingOptions: OptionSet {
    let rawValue: Int
    static let nextDay = ShippingOptions(rawValue: 1 << 0)
    static let secondDay = ShippingOptions(rawValue: 1 << 1)
    static let priority = ShippingOptions(rawValue: 1 << 2)
    static let standard = ShippingOptions(rawValue: 1 << 3)
    }
    ࣮૷͢Δͱ͜͏ͳΔ

    View Slide

  7. self.doSomething(arg, option: [.nextDay, .priority])

    func doSomething(_ arg: Any, option: ShippingOptions) { … }
    ࢖͏ଆ͸͜͏

    View Slide

  8. func doSomething(_ arg: Any, option: ShippingOptions) {
    if option.contains(.nextDay) { … }
    if option.contains(. secondDay) { … }
    }
    ͰɺͲͷΦϓγϣϯ͕ࢦఆ͞Ε͔ͨͰ
    ڍಈΛม͑Δʜ

    View Slide

  9. View Slide

  10. ͳΜ͔Φϓγϣϯ൑ఆ͍͚ͨͩ͠ʹͯ͠͸
    هड़͕໘౗͡Όͳ͍ʁ
    struct ShippingOptions: OptionSet {
    let rawValue: Int
    static let nextDay = ShippingOptions(rawValue: 1 << 0)
    static let secondDay = ShippingOptions(rawValue: 1 << 1)
    static let priority = ShippingOptions(rawValue: 1 << 2)
    static let standard = ShippingOptions(rawValue: 1 << 3)
    }

    View Slide

  11. struct ShippingOptions: OptionSet {
    let rawValue: Int
    static let nextDay = ShippingOptions(rawValue: 1 << 0)
    static let secondDay = ShippingOptions(rawValue: 1 << 1)
    static let priority = ShippingOptions(rawValue: 1 << 2)
    static let standard = ShippingOptions(rawValue: 1 << 3)
    }
    ͳΜ͔Φϓγϣϯ൑ఆ͍͚ͨͩ͠ʹͯ͠͸
    هड़͕໘౗͡Όͳ͍ʁ
    ͜ͷ͋ͨΓ͕ɻ

    View Slide

  12. FOVNͰ΍ͬͯΈΑ͏

    View Slide

  13. 0QUJPOఆٛˠγϯϓϧFOVN
    enum ShippingOptions {
    case nextDay
    case secondDay
    case priority
    case standard
    }

    View Slide

  14. self.doSomething(arg, option: .nextDay)

    func doSomething(_ arg: Any, option: ShippingOptions) { … }
    ࢖͏ଆ͕͜͏

    View Slide

  15. ͜ͷ··ͩͱ
    ෳ਺Φϓγϣϯࢦఆ͕ग़དྷͳ͍ͷͰʜ

    View Slide

  16. Մม௕Ҿ਺
    ʢ7BSJBEJD1BSBNFUFSTʣ

    View Slide

  17. ͜͏ͩͬͨͷΛ
    self.doSomething(arg, option: .nextDay)

    func doSomething(_ arg: Any, option: ShippingOptions) { … }

    View Slide

  18. self.doSomething(arg, option: .nextDay, . priority)

    Մม௕ʹ͢Δ
    func doSomething(_ arg: Any, option: ShippingOptions…) {…}

    View Slide

  19. func doSomething(_ arg: Any, option: ShippingOptions…) {
    if option.contains(.nextDay) { … }
    if option.contains(. secondDay) { … }
    }
    ࣮૷ଆʹ͸"SSBZ͕དྷΔͷͰมߋͳ͠

    View Slide

  20. ·ͱΊ
    ϝιουͷΦϓγϣϯΛ
    ؆୯ʹఆ͍͚ٛͨͩ͠Ͱ͋Ε͹ΞϦͳͷͰ͸

    View Slide

  21. ͓ΘΓɻ

    View Slide