Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modern language support += but not &&=

Most (but I only actually know a small number so lets not get picky on that point) high level languages support multiple different assignment operators.

a += 5;  // increment a and assign result beack to a.

But none (that I have looked at (again a small number)) seem to support the &&= operator.
The reason I ask I recently saw this:

// simplified.
bool x = false;


x = x && testA(); // perform test A/B/C/D stop on first failure.
x = x && testB();
x = x && testC();
x = x && testD();

And I was wondering why we could not use:

x &&= testA(); // perform test A/B/C/D stop on first failure.
x &&= testB();
x &&= testC();
x &&= testD();

The reason being that &&= is not supported in C/C++ which made we think why.

Is there a logical (no pun intended) reason why the language supports all the other major operators with an assignment form but not the &&= or ||=

I have a vague recollection for an argument against these, but google and SO searches are hard when your search term is '&&=' and as a result I found nothing.

like image 569
Martin York Avatar asked Oct 19 '11 21:10

Martin York


1 Answers

&& and || are special in the sense that they are, in most languages, the only constructions that provide short circuiting evaluation so it kind of makes sense for them to be treated differently from the normal, non short-circuiting, operators when it comes to the abbreviated forms. The easiest way to avoid the confusion of &&= short-circuiting or not is not including it in the first place.

like image 198
hugomg Avatar answered Nov 11 '22 02:11

hugomg