Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to test an OptionSet with a switch statement?

Tags:

swift

Defining 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)
}

Call 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 is:

Default
Contains A
Contains B

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

like image 771
GoldenJoe Avatar asked Sep 02 '17 19:09

GoldenJoe


1 Answers

You can't do this directly because switch is using Equatable and, I think, is using SetAlgebra.

However, you can wrap the OptionSet with something like:

public struct TestSetEquatable<T: OptionSet>: Equatable {

    let optionSet: T

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

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

Which lets you 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'm not inclined to do use this code myself but if I had to, this is what I would do.

like image 80
Scott McKenzie Avatar answered Nov 17 '22 08:11

Scott McKenzie