Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NOT condition in 'if case' statement

Tags:

swift

I have an enum:

enum E {
    case A, B, C(Int)
}

let a: E = .A

Here's how I would check if a equals .B

if case .B = a {
    // works fine
}

But how do I check the opposite condition? (a not equal .B)? Here's what I tried:

if case .B != a { // Variable binding in a condition requires an initializer
    // ...
}

if !(case .B = a) { // Expected ',' separator
    // ...
}

Of course, I could do it this way:

if case .B = a {
    // ...
} else {
    // put your code here
}

but this is awkward, as well as using switch statement. Are there any better options?


EDIT: The solution @Greg suggested works if cases do not have associated values, but if they do == operator needs to be overloaded. Sorry for not clarifying this in the first place.

like image 215
Andrii Chernenko Avatar asked Nov 12 '15 16:11

Andrii Chernenko


People also ask

How do you use not in if condition?

OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

How do you use not in an if statement in Python?

The 'not' is a Logical operator in Python that will return True if the expression is False. The 'not' operator is used in the if statements. If x is True, then not will evaluate as false, otherwise, True.

Is not condition in Java?

The not operator is a logical operator, represented in Java by the ! symbol. It's a unary operator that takes a boolean value as its operand. The not operator works by inverting (or negating) the value of its operand.

Can we give condition inside switch case?

Switch Case In C In a switch statement, we pass a variable holding a value in the statement. If the condition is matching to the value, the code under the condition is executed. The condition is represented by a keyword case, followed by the value which can be a character or an integer. After this, there is a colon.

What is the difference between case and if statements?

The CASE statement finds the first matching expression and uses that. This example shows how the CASE statement is used in a WHERE clause. This example shows all customers who live in North America, using the CASE statement to restrict the records. What’s an IF Statement?

Is there an IF statement in SQL Server?

It’s SQL Server only. The CASE statement should let you do whatever you need with your conditions. In Oracle, there is no “IF” statement or keyword specifically in Oracle. If you want to use IF logic, then use the CASE statement.

What is the use of case statement?

The case statement tests the input value until it finds the corresponding pattern and executes the command linked to that input value. Thus, it is an excellent choice for creating menus where users select an option which triggers a corresponding action.

How to use case statement in SQL with a simple expression?

In a simple case statement, it evaluates conditions one by one. Once the condition and expression are matched, it returns the expression mentioned in THEN clause. We have following syntax for a case statement in SQL with a simple expression ... Usually, we store abbreviations in a table instead of its full form.


3 Answers

This "answer" is nothing more than writing your awkward solution in a more compact manner. If you only care about the case when a value is not of a certain enum value, you could write it like this all in one line with the else immediately following the empty then clause:

enum E {
    case A, B(String), C(Int)
}

let a: E = .B("Hello")

if case .A = a {} else {
    print("not an A")
}

if case .B = a {} else {
    print("not a B")
}

if case .C = a {} else {
    print("not a C")
}
like image 85
vacawama Avatar answered Oct 21 '22 22:10

vacawama


There aren't any answers yet mentioning the guard statement, introduced by Swift 2, which is a neat addition to the tools above and, if you ask me, the cleanest solution if you can live with the requirement to return from your function or closure within the else-block:

guard case .B = a else {
    // a does not match .B
    return
}

See Apple's "The Swift Programming Language (Swift 2.2): Statements" for more info.

like image 31
epologee Avatar answered Oct 21 '22 20:10

epologee


You are using single = sign which is an assignment operator. You have to use double == which is a comparison one and don't use case .A, use E.A is the right way to do that:

if E.A == a {
    // works fine
    print("1111")
}

if E.B != a {
    // works fine
    print("2222")
}

if E.B == a {
    // works fine
    print("3333")
}

Extended:

To make it works with associated values you have to implement Equatable protocol, example:

extension E: Equatable {}
func ==(lhs: E, rhs: E) -> Bool {
    switch (lhs, rhs) {
        case (let .C(x1), let .C(x2)):
            return x1 == x2
        case (.A, .A):
        return true

     default:
         return false
    }
}

Of course you have to handle all of the possibilities but I think you have an idea.

Extended:

I don't get your comment but this works for me fine:

enum E {
    case A, B, C(Int)
}

extension E: Equatable {}
func ==(lhs: E, rhs: E) -> Bool {
    switch (lhs, rhs) {
        case (let .C(x1), let .C(x2)):
            return x1 == x2
        case (.A, .A):
            return true
        case (.B, .B):
            return true

     default:
         return false
    }
}

let a: E = .A
let b: E = .B
let c: E = .C(11)

if E.A == a {
    // works fine
    print("aaa true")
}
if E.A != a {
    // works fine
    print("aaa false")
}

if E.B == b {
    // works fine
    print("bbb true")
}

if E.B == b {
    // works fine
    print("bbb false")
}

if E.C(11) == c {
    // works fine
    print("ccc true")
}

if E.C(11) != c {
    // works fine
    print("1 ccc false")
}

if E.C(22) != c {
    // works fine
    print("2 ccc false")
}
like image 9
Greg Avatar answered Oct 21 '22 21:10

Greg