2

Consider this code...

switch(testValue)
{
    case .ValueA,
         .ValueB,
         .ValueC:

        return 40

    default:

        return 0
}

Now if I was just checking for a single enumeration value, I could do this...

return (testValue == .ValueA)
    ? 40
    : 0;

But I'm wondering how I can have something like the latter, but testing for multiples like the former, similar to this pseudo-code...

return (testValue is in [.ValueA, .ValueB, .ValueC])
    ? 40
    : 0;

I know I can do it with an inline array, like so...

return ([SomeEnum.ValueA, .ValueB, .ValueC].Contains(testValue))
    ? 40
    : 0;

...but I'm hoping there's something even cleaner. Is there?

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • Swift can infer the type from the variable you are comparing so `return ([.ValueA, .ValueB, .ValueC].contains(testValue)) ? 40 : 0` should suffice. – vacawama Jul 12 '16 at 19:19
  • 1
    You don't need the parens either: `return [.ValueA, .ValueB, .ValueC].contains(testValue) ? 40 : 0`. – vacawama Jul 12 '16 at 19:31
  • I for one find it very interesting that we may provide multiple matches for a single case in a `switch` statement (`case .ValueA, .ValueB, .ValueC: ...`), whereas we (as far as I can tell) cannot do this for the equivalent `case` pattern matching in an `if case` pattern matching. I believed `if case` pattern matching to be equivalent to a single `case` pattern matching within a `switch` statement (with the only difference that the `case` pattern matching in `switch` statements keeps track of the cases w.r.t. exhaustiveness of the `switch` statement), bit it would seem that I was mistaken? – dfrib Jul 12 '16 at 20:45
  • @dfri I actually said the same thing to Apple as a bug report. My example was: "Although `case 1,3,5,7,9` is a legal pattern in a switch case, it is not a legal pattern in an if case. It should be. That's the bug." They closed it on the grounds that this "behaves as expected". – matt Jul 12 '16 at 21:29
  • @matt thanks for having a look, I see, and agree; nothing in the docs (as far as I've found) states that the should be anything but equivalent in this matter (the closest I can get myself is crazy verbose and not really `if case` at all ..., `if ([.ValueA, .ValueB, .ValueC].reduce(false) { $0 || (testValue ~= $1) }) { /* ... */ }`). Thanks! W.r.t. ninja-edit: their answer, on the other hand, was not very verbose ... – dfrib Jul 12 '16 at 21:32
  • @dfri If you want to file a bug report of your own, feel free to reference mine, which is 22692035. Personally I feel that without this equivalence, the utility of `if case` is reduced to almost nothing. Alternatively, you might want to raise this issue on swift-evolution and see if you can get it elevated to an actual proposal. – matt Jul 12 '16 at 21:33
  • @matt I might just do that, might at least possibly prompt an actual explanation or update of the docs; I'll have a look at it tomorrow. Thanks again, and goodnight (in Europe ...)! – dfrib Jul 12 '16 at 21:35

1 Answers1

0
extension SomeEnum {
    func isOneOf(values: Value...) -> Bool {
        return values.contains(self)
    }
}

return testValue.isOneOf(.ValueA, .ValueB, .ValueC) ? 40 : 0

Evan Liu
  • 111
  • 2