Modern language support + = but not && =

Most (but I actually know a small number, so let's not understand this point). High-level languages ​​support several different assignment operators.

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

But none (which I looked over (again a small number)) seems 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 is that && = is not supported in C / C ++, which made us think why.

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

I have a vague recollection of the argument against them, but google and SO searches are difficult when your search query is "& & =", and as a result I did not find anything.

+6
source share
5 answers

&& and || are special in the sense that in most languages ​​they are the only constructs that provide an assessment of a short circuit, therefore it is reasonable for them to be interpreted differently than normal when it comes to abbreviated forms. The easiest way to avoid confusion &&= short circuit or not, does not include it in the first place.

+1
source

Perl supports this. It is very convenient to write:

 $x ||= $defaultX; 
+2
source

The first reason is that languages ​​allow you to achieve your goal using the "detailed" form. After that, he disapproves of adding operators for each unusual case. One reason is that the language designer should wisely discuss operator priorities. It's not as simple as it sounds: take C as an example that messes up intuitive priorities with & , | , && and || .

The second reason is that most modern languages ​​support some form of exception. Your case can be easily rewritten using exceptions with the added bonus that denial information can be easily transferred to the caller.

Therefore, the need for such a special operator as this is quite small.

Oh, I almost missed the most important reason: C doesn't have them. What this means is best described in a Brief, Incomplete, and Mostly Incorrect History of Programming Languages :

1970 - Nicklaus Wirth creates Pascal, a procedural language. Critics immediately denounce Pascal because it uses the syntax "x: = x + y" instead of the more familiar C-like "x = x + y". This criticism occurs even though C has not yet been invented.

1972 - Dennis Ritchie invents a powerful weapon that shoots back and forth simultaneously. Unsatisfied with the number of deaths and permanent injury from this invention, he invents C and Unix.

It just shows that C is still an attractor that you cannot avoid.

+1
source

Unlike + = and friends, && = does not map to a single hardware instruction. This is a kind of test clarity.

0
source

When will you really need it? Typically, when you perform logical operations, you are not updating the values, but rather calculating something for a one-time operation. Your example is not very realistic; there are not many cases when you really do it.

In addition, in all languages ​​that I can think of extended assignment, exists only for bitwise and arithmetic operations, never performed logical operations.

-1
source

Source: https://habr.com/ru/post/899645/


All Articles