1

Say you need to determine the actual associated type of an enum.

So, a situation like

enum MessageItem: Decodable {

    case a(Images)
    case b(Text)
    case c(Reply)
    ...
}

I used to have code like this

       xSome = x.filter {
            switch $0 {
            case .a(_):
                return false
            case .b(_):
                return true
            case .c(_):
                return true
            }
        }

But then it was possible to have code like this

       xSome = x.filter {
            if case .a = $0 { return false }
            return true
        }

Is there now some way it Swift to compare against associated type producing a boolean?

So, something like:

       xSome = x.filter {
            return (case .a = $0)
        }

So, something like anEnum.is( .someCase )

Is anything like this now in Swift?

(Naturally, I mean without adding a var in the enum, which of course you can do.)

1
  • You can say anEnum == .someCase. What's the situation where that won't do? If it because what you really want to compare is the associated value? Then you can say anEnum == .someCaseWithAssociatedValue(theValueToTestAgainst). Enums are equatable if you declare them Equatable. Commented Oct 31, 2019 at 18:15

1 Answer 1

1

Enums with associated values are equatable if you declare them Equatable. Here's the state of play:

enum MyEnum : Equatable {
    case hey
    case ho
    case heyNonnyNo(String)
}
let e = MyEnum.hey
e == .hey // true
e == .ho // false
// e == .heyNonnyNo // blap, illegal

let e2 = MyEnum.heyNonnyNo("hello")
e2 == .heyNonnyNo("hello") // true
e2 == .heyNonnyNo("goodbye") // true

Why is e == .heyNonnyNo illegal? Because it's unclear what it can mean. This case has an associated value; its value is the associated value. So we can check whether two instances of this case have the same associated value, but we can't just ask (using ==) whether an instance is some associated value of this case.

So if that's what we want to know, we are back to if case:

if case .heyNonnyNo = e2 {
    print("it's a hey nonny no")
}

But you can't say that without if (for use in a conditional) because if case is the keyword; case can't exist by itself. If you really need a Bool, you could write it out like this:

let ok : Bool = {
    switch e2 {
    case .heyNonnyNo: return true
    default: return false
    }
}()
Sign up to request clarification or add additional context in comments.

3 Comments

ahhh! "if case" is a keyword; that's a great fact
To prove that if case is the keyword, try wrapping everything after if in parentheses. You can't. The word case is not the start of a Boolean expression. — I'm not saying your idea is a bad idea, not at all; I'm just describing the state of the language.
Right on, awesome info. Yeah I realize the question wasn't explicit enough, I edited it. Looks like "if case" is the state of the art!! It is somewhat strange there isn't something along the lines of anEnum.isCase( .someCase ) ; maybe in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.