enum as Option. potatotips #52
FOVNBT0QUJPO6/21 potatotips #52taiki komaba(@r_plus)
View Slide
UBJLJLPNBCB!S@QMVT
0QUJPO4FU
CJUຖʹΦϓγϣϯΛׂͯΔΦϓγϣϯͷͨΊͷͷͬΆ͍
public extension DispatchQueue {public struct Attributes : OptionSet {public let rawValue: UInt64public 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
struct ShippingOptions: OptionSet {let rawValue: Intstatic 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)}࣮͢Δͱ͜͏ͳΔ
self.doSomething(arg, option: [.nextDay, .priority])…func doSomething(_ arg: Any, option: ShippingOptions) { … }͏ଆ͜͏
func doSomething(_ arg: Any, option: ShippingOptions) {if option.contains(.nextDay) { … }if option.contains(. secondDay) { … }}ͰɺͲͷΦϓγϣϯ͕ࢦఆ͞Ε͔ͨͰڍಈΛม͑Δʜ
ͳΜ͔Φϓγϣϯఆ͍͚ͨͩ͠ʹͯ͠هड़͕໘͡Όͳ͍ʁstruct ShippingOptions: OptionSet {let rawValue: Intstatic 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)}
struct ShippingOptions: OptionSet {let rawValue: Intstatic 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)}ͳΜ͔Φϓγϣϯఆ͍͚ͨͩ͠ʹͯ͠هड़͕໘͡Όͳ͍ʁ͜ͷ͋ͨΓ͕ɻ
FOVNͰͬͯΈΑ͏
0QUJPOఆٛˠγϯϓϧFOVNenum ShippingOptions {case nextDaycase secondDaycase prioritycase standard}
self.doSomething(arg, option: .nextDay)…func doSomething(_ arg: Any, option: ShippingOptions) { … }͏ଆ͕͜͏
͜ͷ··ͩͱෳΦϓγϣϯࢦఆ͕ग़དྷͳ͍ͷͰʜ
ՄมҾʢ7BSJBEJD1BSBNFUFSTʣ
͜͏ͩͬͨͷΛself.doSomething(arg, option: .nextDay)…func doSomething(_ arg: Any, option: ShippingOptions) { … }
self.doSomething(arg, option: .nextDay, . priority)…Մมʹ͢Δfunc doSomething(_ arg: Any, option: ShippingOptions…) {…}
func doSomething(_ arg: Any, option: ShippingOptions…) {if option.contains(.nextDay) { … }if option.contains(. secondDay) { … }}࣮ଆʹ"SSBZ͕དྷΔͷͰมߋͳ͠
·ͱΊϝιουͷΦϓγϣϯΛ؆୯ʹఆ͍͚ٛͨͩ͠Ͱ͋ΕΞϦͳͷͰ
͓ΘΓɻ