Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. 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
  2. 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) } ࣮૷͢Δͱ͜͏ͳΔ
  3. func doSomething(_ arg: Any, option: ShippingOptions) { if option.contains(.nextDay) {

    … } if option.contains(. secondDay) { … } } ͰɺͲͷΦϓγϣϯ͕ࢦఆ͞Ε͔ͨͰ ڍಈΛม͑Δʜ
  4. ͳΜ͔Φϓγϣϯ൑ఆ͍͚ͨͩ͠ʹͯ͠͸ هड़͕໘౗͡Όͳ͍ʁ 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) }
  5. 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) } ͳΜ͔Φϓγϣϯ൑ఆ͍͚ͨͩ͠ʹͯ͠͸ هड़͕໘౗͡Όͳ͍ʁ ͜ͷ͋ͨΓ͕ɻ
  6. func doSomething(_ arg: Any, option: ShippingOptions…) { if option.contains(.nextDay) {

    … } if option.contains(. secondDay) { … } } ࣮૷ଆʹ͸"SSBZ͕དྷΔͷͰมߋͳ͠