Is there a way to test OptionSet using a switch statement?

Definition of a simple OptionSet:

public struct TestSet : OptionSet, Hashable
{
    public let rawValue: Int
    public init(rawValue:Int){ self.rawValue = rawValue}
    public var hashValue: Int {
        return self.rawValue
    }

    public static let A   = TestSet(rawValue: 1 << 0)
    public static let B   = TestSet(rawValue: 1 << 1)
    public static let C   = TestSet(rawValue: 1 << 2)
}

Name it:

let ostest : TestSet = [.A, .B]

switch ostest{
case .A: print("A")
case .B: print("B")
case .C: print("C")
default: print("Default")
}

if ostest.contains(.A){
    print("Contains A")
}
if ostest.contains(.B){
    print("Contains B")
}

Output:

Default
Contains A
Contains B

Is there a way to check if OptionSets contains a value or combination of values ​​with a switch statement? This would be much cleaner than a series of if-contains statements.

+7
source share
1 answer

You cannot do this directly because switch uses Equatable and, I think, uses SetAlgebra.

However, you can wrap something like this in OptionSet:

public struct TestSetEquatable<T: OptionSet>: Equatable {

    let optionSet: T

    public static func == (lhs: Self, rhs: Self) -> Bool {

        return lhs.optionSet.isSuperset(of: rhs.optionSet)
    }
}

What allows you to do:

let ostest : TestSet = [.A, .C]

switch TestSetEquatable(optionSet: ostest) {

  case TestSetEquatable(optionSet: [.A, .B]):
   print("-AB")
   fallthrough

 case TestSetEquatable(optionSet: [.A, .C]):
   print("-AC")
   fallthrough

 case TestSetEquatable(optionSet: [.A]):
   print("-A")
   fallthrough

   default:
     print("-")
}

This prints:

-AC
-A
- // from the fall through to default

Opinion: I am not inclined to use this code myself, but if I had to, this is what I would do.

+1
source

Source: https://habr.com/ru/post/1684959/


All Articles