OptionSet - add anyOf, allOf, noneOf methods

OptionSets seem to be an ideal, compact mechanism for managing a collection of feature flags. However, the available technique for checking for a feature (set.contains(.flag)) is really only useful for testing one flag at a time.

In real life, it is often useful to test for a more complex combination of feature flags. (For example, "take some action is feature ".a" or ".b" is present.) This is currently cumbersome.

I would like to propose extending the OptionSet object to include more complex types of tests (specifically geared to testing multiple options at once). The most obvious additions are
anyOf([.a, .b, ...])
allOf([.a, .b, .c, ...])
noneOf([.a, .b, ...])

although one could obviously choose other method signatures.

Cheers,

Rick Aurbach

These already exist under the names:

x.isDisjoint(with: y)  // i.e., x contains none of y
!x.isDisjoint(with: y) // i.e., x contains some of y
x.isSuperset(of: y)    // i.e., x contains all of y

// Also available:
x.isSubset(of: y)
x.isStrictSubset(of: y)
x.isStrictSuperset(of: y)

For all of the available options, see the documentation for SetAlgebra, which is refined by OptionSet. The naming of these APIs was a contentious process that went through Swift Evolution as proposal SE-0059 in 2016.

5 Likes

Excellent point. I should have read the SetAlgebra documentation more carefully.

Please excuse the waste of bandwidth.