Tuple Comparision

I was trying to evaulate

let myTuple = ("blue", false) let otherTuple = ("blue", true) if myTuple < otherTuple { print("yes it evaluates") }

Ans I got

/tmp/S9jAk7P7KW/main.swift:5:12: error: binary operator '<' cannot be applied to two '(String, Bool)' operands if myTuple < otherTuple {



My question is why there is no compile time issue in first place where the declaration is

let myTuple = ("blue", false)
                        ~~~~~~

something like above


Answered by Claude31 in 847630022

true and false are not 1 and 0 as in other languages.

But of course, this works

let myTuple = ("blue", 0)
let otherTuple = ("blue", 1)
if myTuple < otherTuple { print("yes it evaluates") }

and this as well:

let myTuple2 = ("blue", false)
let otherTuple2 = ("blue", true)
if myTuple2 == otherTuple2 {
    print("yes it evaluates")
} else {
    print("Not equal")
}

You may be interested in reading this discussion in Swift forum: https://forums.swift.org/t/why-bool-doesnt-conform-to-comparable/10698/6

I'm not sure what you're trying to achieve here?

Why would false be less than true? It makes no sense.

Anyway, I get the same error in Xcode as I type out your code, which is a compile-time error because Xcode is compiling the code as I type, and it found this problem.

Accepted Answer

true and false are not 1 and 0 as in other languages.

But of course, this works

let myTuple = ("blue", 0)
let otherTuple = ("blue", 1)
if myTuple < otherTuple { print("yes it evaluates") }

and this as well:

let myTuple2 = ("blue", false)
let otherTuple2 = ("blue", true)
if myTuple2 == otherTuple2 {
    print("yes it evaluates")
} else {
    print("Not equal")
}

You may be interested in reading this discussion in Swift forum: https://forums.swift.org/t/why-bool-doesnt-conform-to-comparable/10698/6

Tuple Comparision
 
 
Q