Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error why matching an enumeration using a if statement [duplicate]

I face a problem using enumeration I can't understand.

Here is declaration of an enumeration type:

enum SomeType {
    case un
    case deux
    case trois
}

Then I want to match an individual enumeration values with a if statement:

var testValue: SomeType = .trois

if testValue == .trois {
    // Do something
}

Everything is fine!

Now I want to add an associated value only to the first member value:

enum SomeType {
    case un(Int)
    case deux
    case trois
}

var testValue: SomeType = .trois

if testValue == .trois {
    // Do something
}

An error than appear on the if statement: Could not find member 'trois'

Does this mean enumerations can only be matched using a switchstatement?

Precisions
What I want to achieve is: "Does testValue is of member value of 'trois' with no consideration for associated value". In others words, how to match an enumeration on member value only.

Here a solution implementing Airspeed Velocity answers:

// Test equality only on member value
func == (lhs:SomeType, rhs:SomeType) -> Bool {
    switch (lhs, rhs) {
    case (.un(let lhsNum), .un(let rhsNum)):return true
    case (.deux, .deux): return true
    case (.trois, .trois): return true
    default: return false
    }
}

// Test equality on member value AND associated value
func === (lhs:SomeType, rhs:SomeType) -> Bool {
    switch (lhs, rhs) {
    case (.un(let lhsNum), .un(let rhsNum)) where lhsNum == rhsNum: return true
    case (.deux, .deux): return true
    case (.trois, .trois): return true
    default: return false
    }
}

var testValue = SomeType.un(3)


// Tests

if testValue == .un(1) {
    println("Same member value")
}


if testValue === .un(3) {
    println("Same member value AND same associated contents")
}
like image 284
Dominique Vial Avatar asked Dec 18 '14 19:12

Dominique Vial


People also ask

What is enumeration in Swift explain with example?

In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values. We use the enum keyword to create an enum. For example, enum Season { case spring, summer, autumn, winter } Here, Season - name of the enum.

Can enum have two same values python?

By definition, the enumeration member values are unique. However, you can create different member names with the same values.

Why use enum in Swift?

Enumerations (or enums for short) in Swift define a common type for a group of related values. According to the Swift documentation, enums enable you to work with those values in a type-safe way within your code. Enums come in particularly handy when you have a lot of different options you want to encode.

What is enum in Swift ui?

Enumerations – usually just called “enum” and pronounced “ee-num” - are a way for you to define your own kind of value in Swift. In some programming languages they are simple little things, but Swift adds a huge amount of power to them if you want to go beyond the basics.


1 Answers

Enums that don't have associated types are automatically equatable. Enums that have associated types aren't. This makes sense, because only you can know how the associated type (such as the integer that comes with your .un value) should be handled. Even though .trois doesn't have an associated type, the lack of freebie equateableness affects the whole enum. Switch works a little differently, using pattern matching, so it still works.

If you want your enum with an associated type to be equatable, you can define your own == operator:

enum SomeType {
    case un(Int)
    case deux
    case trois
}

// possibly there's a more succinct way to do this switch
func ==(lhs: SomeType, rhs: SomeType) -> Bool {
    switch (lhs,rhs) {
    case let (.un(i), .un(j)) where i == j: return true
    case (.deux,.deux): return true
    case (.trois, .trois): return true
    default: return false
    }
}

var testValue: SomeType = .trois

if testValue == .trois {
    println("equals .trois")
}

// note, for SomeType to work with generic
// functions that require Equatable, you have
// to add that too:
extension SomeType: Equatable { }

// which means this will work:
let a: [SomeType] = [.un(1), .deux, .trois]
find(a, .trois)
like image 112
Airspeed Velocity Avatar answered Nov 13 '22 20:11

Airspeed Velocity