[Pitch] `@OptionSet` macro

I went ahead and implemented something similar to your idea. The result is this:

@OptionSet<UInt8>
struct ShippingOptions {
  static var nextDay: ShippingOptions
  static var secondDay: ShippingOptions
  static var priority: ShippingOptions
  static var standard: ShippingOptions

  static let express: ShippingOptions = [.nextDay, .secondDay]
  static let all: ShippingOptions = [.express, .priority, .standard]
}

The implementation wasn't hard, and turns each of those non-initialized static properties into computed properties, e.g.,

static var nextDay: ShippingOptions {
  get {
    Self(rawValue: 1 << 0)
  }
}

One of the nice things about @Joe_Groff 's formulation here is that you can go ahead and put availability on the static variables, along with comments, access control, and anything else. This is one of the advantages of this "fill in the details" approach vs. what was originally proposed.

Doug

20 Likes