What is the recommended way of testing if a value does not correspond to an enumeration case with an associated value?
Given the following declarations:
enum Associated {
case text(String)
case number(Float)
case flag(Bool)
}
let t = Associated.text("my text")
Testing whether a value matches a specific case is readable (though awkward because of the implicit assignment):
if case .text(_) = t {
print ("found a text")
}
The opposite, testing whether a value does not match a specific case, is more difficult. The straightforward syntax is not accepted by the compiler:
if !(case .text(_) = t) {
print("not a text")
}
I have found two variants that work: using else leads to quite cryptic code:
if case .text(_) = t {} else {
print("not a text")
}
and the default of a switch multiplies keywords:
switch n { case .text(_): break default:
print("not a text")
}
Some similar questions on the net also suggest a guard, but that is accepted only when followed by return (or break) and thus ignores later statements:
guard case .text(_) = n else {
print("not a text")
return
}
Is there a more readable and elegant way for excluding a case with an associated value?