[Pitch] `@OptionSet` macro

I'm occasionally making use of an extension like this:

extension OptionSet {
	subscript (flag: Element) -> Bool {
		get { contains(flag) }
		set {
			if newValue {
				insert(flag)
			} else {
				remove(flag)
			}
		}
	}
}

This lets me to treat flags as boolean values, almost like properties:

var style: NSWindow.StyleMask = []
style[.borderless] = true
style[.closable].toggle()
style[.resizable] = style[.closable]

I find it's often easier to reason with that than set algebra. So if a macro (or a language mechanism) was to add properties directly so the above can be rewriten as:

var style: NSWindow.StyleMask = []
style.borderless = true
style.closable.toggle()
style.resizable = style.closable

then I'm all for it. But the subscript solution is pretty good too.

9 Likes