[swift-evolution] Remove (!) logical negation operator

Bruno Berisso gojan.wan at gmail.com
Tue Dec 15 08:11:47 CST 2015


Hello,

I'm really used to negate logical expressions with this operator but it
never feels confortable to me. If I need to negate some complex expression
sometimes I prefer to write a temporal variable and then negate that with
*!* because I fear than others, or myself, could overlook it and cause
confusion.

Now that Swift use the same operator to force the unwrap of optional values
it becomes even worse.

Consider this examples:

if someOptionalValue != nil {
    ...
    if !(otherBoolValue && someOptionalValue! > 0) {
        ...
    }
}

Or even worse, what happen when negating Optional<Bool> values?

//Best case
if let value = optionalBool where !value {
    ...
}

//Worse case
if !optionalBool! {
    ...
}

Now what happen with this examples if we instead use a *not* operator:

if someOptionalValue != nil {
    ...
    if not (otherBoolValue && someOptionalValue! > 0) {
        ...
    }
}

if let value = optionalBool where not value {
    ...
}

if not optionalBool! {
    ...
}

I'm not sure if *not* is the right path here but I really want to improve
the *!* to something more clear at a glance and that doesn't have different
behaviour depending where it appears in an expression.

Thanks,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/fe1ae276/attachment.html>


More information about the swift-evolution mailing list