Conditional let as a Boolean value

#1

Why conditional let isn't considered as a Boolean value? To me it looks quite natural to write something like:

while let myInt = iterator.next() as? Int {
if myInt > 666 { ... }
}

but nope, the compiler doesn't accept it.

To overcome that I can see nothing better than

while true {
    guard let myInt = iterator.next() as? Int else {break}
    if myInt > 666 {...}
}

which looks really bizzaire to me.

(Alexander Momchilov) #2

Works fine for me:

let testData: [Any] = [1, 10, 100, 1_000, 10_000]
var iterator = testData.makeIterator()

while let myInt = iterator.next() as? Int {
    if myInt < 666 { print("\(myInt) is over 666") }
}
1 Like
#3

Indeed works, sorry. Actually I replaced that code, so I can't figure out, where my problem was.