Add XOR (Exclusive OR) operator support for Bool type

Yes I mean that != doesn't look like XOR. I mean it isn't an XOR operator, it is a very general operator that is available for tons of types and means "not equal". If you look in the docs it's not even explained, and it is placed separately from && and || which are explained. The only mention of != is in the documentation for == where it says:

/// Equality is the inverse of inequality. For any values `a` and `b`,
/// `a == b` implies that `a != b` is `false`.

So I don't think it is fair to say we already have XOR. We just have an unrelated operator that happens to have the same truth table as XOR.

It might be a well known workaround among C-developers, but shouldn't Swift aim to be accessible for new developers too? At the very least the docs and the official Swift book should reflect that != is viewed as XOR I think.

Maybe I'm the only one, but I certainly didn't know of the != trick when I came to iOS development. I did come to use != sometimes when it fit the behaviour I wanted, and only several years later I realised that it was actually the same as, and an acknowledged synonym of XOR.

Maybe this is because I don't have a C background, but I think such developers will be increasingly common.

But I am not sure that it's a strong argument to say that you can get by without it. You can get by without Bool.toggle() and Int.isMultiple(of:) too, and a lot of other convenient methods recently introduced.

1 Like