Conditional XOR ^^ and XNOR (iff) !^^ operators

Topics: C# Language Design
Apr 9, 2014 at 3:03 PM
Edited Apr 9, 2014 at 3:12 PM

Conditional XOR Operator

For better expressivity, please introduce ^^ XOR operator to the language.

See http://stackoverflow.com/a/14664795/863980

In bool struct, define ^^ operator like:
public static bool operator^^ (bool right, bool left) 
{ 
    return right != left; 
}

Conditional XNOR Operator

Also, the XNOR ( iff; if and only if ) conditional operator in C#:
public static operator!^^ (bool right, bool left) 
{ 
    return right == left; 
    // Or 
    // return !(right ^^ left); 
    // whichever is better performance-wise.
}
Thanks.
Apr 9, 2014 at 3:10 PM
Edited Apr 9, 2014 at 3:16 PM
TheDeeds wrote:
For better expressivity, please introduce ^^ XOR operator to the language.
Could you please show any example where a ^^ b is much more expressive than a != b?
(For iff I am quite sure that == is much more intuitive and expressive than !^^.)

Edit:
By the way, XOR for bools exists in the language already, having the form ^.
Apr 9, 2014 at 3:52 PM
@VladD, thanks for reverting.

Mostly in state-machine scenarios, this might come handy. But I have a better (perhaps convincing) example.

Given the lack of parameter alias support in C# (one of the neat feature present in PowerShell), lets consider this scenario:
void SomeFunction( Func<XType, YType> exampleProcess, string flattenedVersionOfFirstParameter )
{
    if( ( exampleProcess != null && flattenedVersionOfFirstParameter == null ) ||
        ( exampleProcess == null && flattenedVersionOfFirstParameter != null ) )
        doThat(_, __);

    // OR     if( ( ( exampleProcess == null ) != ( flattenedVersionOfFirstParameter == null ) ) )
}
would be written as:
void SomeFunction( Func<XType, YType> exampleProcess, string flattenedVersionOfFirstParameter )
{
    if( ( exampleProcess == null ^^ flattenedVersionOfFirstParameter == null ) )
        doThat(_, __);
}
This gives a clear message. Same goes for XNOR (iff).

IMO, its more about clarification of intent in natural -- symbolic -- way.
Apr 12, 2014 at 4:54 PM
Edited Apr 12, 2014 at 4:55 PM
Clearly the two operators you request already exist and are called != and ==.

Even if you don’t want to use != for some reason, clearly you should use ^ instead which already exists, and you can use !(a ^ b) for the other. I very much feel that == and != are much clearer though, including in your example:
// if the nullness of foo is the same as that of bar
if ((foo == null) == (bar == null))

// if the nullness of foo is different from that of bar
if ((foo == null) != (bar == null))