Skip to main content

Comparison Operators

Comparison operators work with boolean and integer values.

Equality ==

  • Equality: == is supported for booleans, numbers, addresses, strings, characters, enums, paths, Type values, references, and Void values (()). Variable-sized arrays, fixed-size arrays, dictionaries, and optionals also support equality tests if their inner types do.

    Both sides of the equality operator may be optional, even of different levels; for example, it is possible to compare a non-optional with a double-optional (??):


    _10
    1 == 1 // is `true`
    _10
    _10
    1 == 2 // is `false`


    _10
    true == true // is `true`
    _10
    _10
    true == false // is `false`


    _10
    let x: Int? = 1
    _10
    x == nil // is `false`


    _10
    let x: Int = 1
    _10
    x == nil // is `false`


    _10
    // Comparisons of different levels of optionals are possible.
    _10
    let x: Int? = 2
    _10
    let y: Int?? = nil
    _10
    x == y // is `false`


    _10
    // Comparisons of different levels of optionals are possible.
    _10
    let x: Int? = 2
    _10
    let y: Int?? = 2
    _10
    x == y // is `true`


    _10
    // Equality tests of arrays are possible if their inner types are equatable.
    _10
    let xs: [Int] = [1, 2, 3]
    _10
    let ys: [Int] = [1, 2, 3]
    _10
    xs == ys // is `true`
    _10
    _10
    let xss: [[Int]] = [xs, xs, xs]
    _10
    let yss: [[Int]] = [ys, ys, ys]
    _10
    xss == yss // is `true`


    _10
    // Equality also applies to fixed-size arrays. If their lengths differ, the result is a type error.
    _10
    let xs: [Int; 2] = [1, 2]
    _10
    let ys: [Int; 2] = [0 + 1, 1 + 1]
    _10
    xs == ys // is `true`


    _10
    // Equality tests of dictionaries are possible if the key and value types are equatable.
    _10
    let d1 = {"abc": 1, "def": 2}
    _10
    let d2 = {"abc": 1, "def": 2}
    _10
    d1 == d2 // is `true`
    _10
    _10
    let d3 = {"abc": {1: {"a": 1000}, 2: {"b": 2000}}, "def": {4: {"c": 1000}, 5: {"d": 2000}}}
    _10
    let d4 = {"abc": {1: {"a": 1000}, 2: {"b": 2000}}, "def": {4: {"c": 1000}, 5: {"d": 2000}}}
    _10
    d3 == d4 // is `true`

Inequality !=

  • Inequality: != is supported for booleans, numbers, addresses, strings, characters, enums, paths, Type values, references, and Void values (()). Variable-sized arrays, fixed-size arrays, dictionaries, and optionals also support inequality tests if their inner types do.

    Both sides of the inequality operator may be optional, even of different levels; for example, it is possible to compare a non-optional with a double-optional (??):


    _10
    1 != 1 // is `false`
    _10
    _10
    1 != 2 // is `true`


    _10
    true != true // is `false`
    _10
    _10
    true != false // is `true`


    _10
    let x: Int? = 1
    _10
    x != nil // is `true`


    _10
    let x: Int = 1
    _10
    x != nil // is `true`


    _10
    // Comparisons of different levels of optionals are possible.
    _10
    let x: Int? = 2
    _10
    let y: Int?? = nil
    _10
    x != y // is `true`


    _10
    // Comparisons of different levels of optionals are possible.
    _10
    let x: Int? = 2
    _10
    let y: Int?? = 2
    _10
    x != y // is `false`


    _10
    // Inequality tests of arrays are possible if their inner types are equatable.
    _10
    let xs: [Int] = [1, 2, 3]
    _10
    let ys: [Int] = [4, 5, 6]
    _10
    xs != ys // is `true`


    _10
    // Inequality also applies to fixed-size arrays. If their lengths differ, the result is a type error.
    _10
    let xs: [Int; 2] = [1, 2]
    _10
    let ys: [Int; 2] = [1, 2]
    _10
    xs != ys // is `false`


    _10
    // Inequality tests of dictionaries are possible if the key and value types are equatable.
    _10
    let d1 = {"abc": 1, "def": 2}
    _10
    let d2 = {"abc": 1, "def": 500}
    _10
    d1 != d2 // is `true`
    _10
    _10
    let d3 = {"abc": {1: {"a": 1000}, 2: {"b": 2000}}, "def": {4: {"c": 1000}, 5: {"d": 2000}}}
    _10
    let d4 = {"abc": {1: {"a": 1000}, 2: {"b": 2000}}, "def": {4: {"c": 1000}, 5: {"d": 2000}}}
    _10
    d3 != d4 // is `false`

Less than <

  • Less than: < is supported for integers, booleans, characters, and strings:


    _23
    1 < 1 // is `false`
    _23
    _23
    1 < 2 // is `true`
    _23
    _23
    2 < 1 // is `false`
    _23
    _23
    false < true // is `true`
    _23
    _23
    true < true // is `false`
    _23
    _23
    "a" < "b" // is `true`
    _23
    _23
    "z" < "a" // is `false`
    _23
    _23
    "a" < "A" // is `false`
    _23
    _23
    "" < "" // is `false`
    _23
    _23
    "" < "a" // is `true`
    _23
    _23
    "az" < "b" // is `true`
    _23
    _23
    "xAB" < "Xab" // is `false`

Less or equal than <=

  • Less or equal than: <= is supported for integers, booleans, characters, and strings:


    _25
    1 <= 1 // is `true`
    _25
    _25
    1 <= 2 // is `true`
    _25
    _25
    2 <= 1 // is `false`
    _25
    _25
    false <= true // is `true`
    _25
    _25
    true <= true // is `true`
    _25
    _25
    true <= false // is `false`
    _25
    _25
    "c" <= "a" // is `false`
    _25
    _25
    "z" <= "z" // is `true`
    _25
    _25
    "a" <= "A" // is `false`
    _25
    _25
    "" <= "" // is `true`
    _25
    _25
    "" <= "a" // is `true`
    _25
    _25
    "az" <= "b" // is `true`
    _25
    _25
    "xAB" <= "Xab" // is `false`

Greater than >

  • Greater than: > is supported for integers, booleans, characters, and strings:


    _25
    1 > 1 // is `false`
    _25
    _25
    1 > 2 // is `false`
    _25
    _25
    2 > 1 // is `true`
    _25
    _25
    false > true // is `false`
    _25
    _25
    true > true // is `false`
    _25
    _25
    true > false // is `true`
    _25
    _25
    "c" > "a" // is `true`
    _25
    _25
    "g" > "g" // is `false`
    _25
    _25
    "a" > "A" // is `true`
    _25
    _25
    "" > "" // is `false`
    _25
    _25
    "" > "a" // is `false`
    _25
    _25
    "az" > "b" // is `false`
    _25
    _25
    "xAB" > "Xab" // is `true`

Greater or equal than >=

  • Greater or equal than: >= is supported for integers, booleans, characters, and strings:


    _25
    1 >= 1 // is `true`
    _25
    _25
    1 >= 2 // is `false`
    _25
    _25
    2 >= 1 // is `true`
    _25
    _25
    false >= true // is `false`
    _25
    _25
    true >= true // is `true`
    _25
    _25
    true >= false // is `true`
    _25
    _25
    "c" >= "a" // is `true`
    _25
    _25
    "q" >= "q" // is `true`
    _25
    _25
    "a" >= "A" // is `true`
    _25
    _25
    "" >= "" // is `true`
    _25
    _25
    "" >= "a" // is `true`
    _25
    _25
    "az" >= "b" // is `true`
    _25
    _25
    "xAB" >= "Xab" // is `false`

Comparing number super-types

Similar to arithmetic operators, comparison operators are also not supported for number supertypes (Number, SignedNumber FixedPoint, SignedFixedPoint, Integer, and SignedInteger), as they may or may not succeed at run-time:


_10
let x: Integer = 3 as Int8
_10
let y: Integer = 4 as Int8
_10
_10
let z: Bool = x > y // Static error

Values of these types must be cast to the desired type before performing the arithmetic operation:


_10
let z: Bool = (x as! Int8) > (y as! Int8)