Hi Magnus,

when there was talk here about a ^^ operator for logical eXclusive OR, I was looking for precedence in other languages.


There are two differences in Raku regarding shortcut operators and ^ to the usual C like notations:


1. Shortcut Operators return one of the arguments

Raku's shortcut logical operators tests for True- or False-ness of their arguments, but return one of the original arguments.

However, Raku's non-shortcut logical operators tests for True- or False-ness of their arguments and return a boolean True or False.


2. Both ^ and ^^ test for exactly one True value instead of for an odd number of True values (parity)

This makes ^^ a true short-cut operator, as it stops (and ^ returns False and ^^ returns Nil), after/if a second true value has been encountered


I would not propose to change the existing well-established operators in C++.


But this is an interesting functionality:

 - && returns the first False argument (otherwise the last argument)

 - || returns the first True argument (otherwise the last argument)

 - ^^ returns the only True argument (or Nil if more than one otherwise the last argument)

 - // returns the first defined argument (otherwise the last argument)

 - min returns the smallest argument

 - max returns the largest argument


In C++ speak the original argument is used and recovered after an implicit conversion.

In C++ this can be compared with std::ranges::find_if, std::ranges::find_if_not and std::ranges::minmax with the use of a projection to bool or int.





-----Ursprüngliche Nachricht-----
Von: Magnus Fromreide <magfr@lysator.liu.se>
Gesendet: Do 11.05.2023 08:21
Betreff: Re: [std-proposals] ^^operator [was: Re: New draft proposal: Add "%s" (two-digit truncated-integer seconds) as a std::format conversion specifier for std::chrono time types.]
An: Sebastian Wittmeier via Std-Proposals <std-proposals@lists.isocpp.org>;
CC: Jonathan Wakely <cxx@kayari.org>; Sebastian Wittmeier <wittmeier@projectalpha.org>;
On Thu, May 04, 2023 at 10:54:21AM +0200, Sebastian Wittmeier via Std-Proposals wrote:
> Should be:
> auto operator^^(auto A, auto B)
> {
>     if(A && !B)
>         return A;
>     if(B && !A)
>         return B;
>     return nil;
> }

That is a surprising formulation - not bad but surprising.

Consider operator ||, do you propose to change it to something along the
lines of

auto operator||(auto A, auto B)
if (A)
return A;
return B;

but with extra magic to short-circut?