Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

&&= and ||= operators [duplicate]

Possible Duplicates:
Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)
Why does a “&&=” Operator not exist?

Today at work I wrote the following LOC (the real identities of b and b1 are confidential :)

b &&= b1; // meaning b = b && b1; 

I stared at it for a couple of seconds and realized that there exists no such operator. Just to be sure, I clicked on compile and it failed. To be dead sure I consulted the standard.

Are there specific reasons that there are no such operators? I can think of some:

  1. b &&= b1 and b = b && b1 may not be equivalent because of short-circuit evaluation of &&.
  2. &&= is ugly
  3. &&= is rarely needed

I do not claim that it would be very useful to have such operators, no. I also don't claim that any or all of the three above reasons are not enough to refrain from creating that operator. My question is the following: is there maybe a much more serious reason which I am overseeing?

like image 564
Armen Tsirunyan Avatar asked Oct 18 '10 20:10

Armen Tsirunyan


1 Answers

I don't know why both the question and some of the answers mention short-circuiting behavior of the corresponding logical operators as a potential issue.

There's absolutely no short-circuit-related problems with defining &&= and ||= operators. They should be defined uniformly with += and other similar operators, meaning that a &&= b should be equivalent to a = a && b, but with a being evaluated only once in &&= version. This means in turn that b is not evaluated at all if a is originally zero. Easy.

So, the only reason they don't exist in the language is, well, "just because".

like image 191
AnT Avatar answered Oct 07 '22 18:10

AnT