Jump to page: 1 2
Thread overview
&&=, ||= ?
Jul 12, 2005
z
Jul 12, 2005
Mike Parker
Jul 12, 2005
clayasaurus
Jul 12, 2005
Hasan Aljudy
Jul 12, 2005
Uwe Salomon
Jul 14, 2005
Stewart Gordon
Jul 14, 2005
Uwe Salomon
Jul 14, 2005
Uwe Salomon
Jul 14, 2005
Stewart Gordon
Jul 14, 2005
Uwe Salomon
Jul 15, 2005
Stewart Gordon
Re: &&=, ||= ? (really, LHS evaluated only once)
Jul 18, 2005
Greg Smith
Jul 18, 2005
Greg Smith
Jul 18, 2005
Derek Parnell
Jul 19, 2005
Uwe Salomon
Jul 19, 2005
Derek Parnell
Jul 19, 2005
Uwe Salomon
July 12, 2005
For integer type, we have (+=, -=, *= /=), e.g.

i += j;

For bool type, why we don't have (&&=, ||=) ?

void f() {
bool b, c;
b &&= c;
}

$ dmd -c aae.d
aae.d:5: expression expected, not '='
aae.d:5: found 'c' when expecting ';' following 'statement'

Is there a reason for this?


July 12, 2005
z@gg.com wrote:
> For integer type, we have (+=, -=, *= /=), e.g.
> 
> i += j;
> 
> For bool type, why we don't have (&&=, ||=) ?
> 
> void f() {
> bool b, c;
> b &&= c;
> }
> 
> $ dmd -c aae.d
> aae.d:5: expression expected, not '='
> aae.d:5: found 'c' when expecting ';' following 'statement'
> 
> Is there a reason for this?
> 
> 
What exactly would &&= do? I'm not grokking the assignment part of it.
July 12, 2005
Mike Parker wrote:
> z@gg.com wrote:
> 
>> For integer type, we have (+=, -=, *= /=), e.g.
>>
>> i += j;
>>
>> For bool type, why we don't have (&&=, ||=) ?
>>
>> void f() {
>> bool b, c;
>> b &&= c;
>> }
>>
>> $ dmd -c aae.d
>> aae.d:5: expression expected, not '='
>> aae.d:5: found 'c' when expecting ';' following 'statement'
>>
>> Is there a reason for this?
>>
>>
> What exactly would &&= do? I'm not grokking the assignment part of it.

I assume it would mean this... (using bool/bit bob)

bob = (bob && a);

would become

bob &&= a;

July 12, 2005
z@gg.com wrote:
> For integer type, we have (+=, -=, *= /=), e.g.
> 
> i += j;
> 
> For bool type, why we don't have (&&=, ||=) ?
> 
> void f() {
> bool b, c;
> b &&= c;
> }
> 
> $ dmd -c aae.d
> aae.d:5: expression expected, not '='
> aae.d:5: found 'c' when expecting ';' following 'statement'
> 
> Is there a reason for this?
> 
> 

I think it's just that nobody every needed it or thought about it .. or something like that!

we don't work with booleans and manipulate thier values much .. we just use them for control structures i.e. if .. while .. etc.

only time I work with booleans is as flags .. the most radical manipulation I would do is toggling them
flag = !flag;

July 12, 2005
On Tue, 12 Jul 2005 03:25:34 +0200, <z@gg.com> wrote:

> For integer type, we have (+=, -=, *= /=), e.g.
>
> i += j;
>
> For bool type, why we don't have (&&=, ||=) ?
>
> void f() {
> bool b, c;
> b &&= c;
> }
>
> $ dmd -c aae.d
> aae.d:5: expression expected, not '='
> aae.d:5: found 'c' when expecting ';' following 'statement'
>
> Is there a reason for this?

Yes. For the "bit" type (a && b) and (a & b) are the same, as it is only a single bit. && exists only because integers have more than one bit, but all nonzero values are interpreted as true. Thus, if you want to work with bits, just use &=, |= etc. With integers you have to use the long form, because all op= variants are guaranteed to evaluate the lvalue only once. This is not possible for &&= with, say, an uint.

Ciao
uwe
July 14, 2005
Uwe Salomon wrote:
> On Tue, 12 Jul 2005 03:25:34 +0200, <z@gg.com> wrote:
<snip>
>> For bool type, why we don't have (&&=, ||=) ?
<snip>
> Yes. For the "bit" type (a && b) and (a & b) are the same, as it is only a single bit. && exists only because integers have more than one bit, but all nonzero values are interpreted as true. Thus, if you want to work with bits, just use &=, |= etc.

Unless you want the rvalue to be lazily evaluated.

Really

    b &&= c;

should be equivalent to

    if (b) b = c;

and similarly

    b ||= c;

equivalent to

    if (!b) b = c;

> With integers you have to use the long form, because all op= variants are guaranteed to evaluate the lvalue only once.  This is not possible for &&= with, say, an uint.

What do you mean by this?

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:- a->--- UB@ P+ L E@ W++@ N+++ o K- w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
July 14, 2005
>> With integers you have to use the long form, because all op= variants are guaranteed to evaluate the lvalue only once.  This is not possible for &&= with, say, an uint.
>
> What do you mean by this?

Look at the code equivalent you have given yourself:

> Really
>
>      b &&= c;
>
> should be equivalent to
>
>      if (b) b = c;

if (b) -- first access to b
b = c -- second access to b

But the operators +=  -=  |=  etc. should evaluate b only once, not twice.

By the way, b &&= c should mean something like this:

if (b)
  b = (c != 0);

Ciao
uwe
July 14, 2005
> But the operators +=  -=  |=  etc. should evaluate b only once, not twice.

Though i must add that the generated machine code could be very similar if the compiler does not put the variables in the registers. Both of them load a variable from the stack into a register, change it and write it back to the stack.

Ciao
uwe
July 14, 2005
Uwe Salomon wrote:
>>> With integers you have to use the long form, because all op= variants  are guaranteed to evaluate the lvalue only once.  This is not possible  for &&= with, say, an uint.
>>
>> What do you mean by this?
> 
> Look at the code equivalent you have given yourself:

I still don't see how it relates.

>> Really
>>
>>      b &&= c;
>>
>> should be equivalent to
>>
>>      if (b) b = c;
> 
> if (b) -- first access to b
> b = c -- second access to b
> 
> But the operators +=  -=  |=  etc. should evaluate b only once, not twice.
<snip>

Exactly.  I was using b to refer to the evaluation, rather than the expression.

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:- a->--- UB@ P+ L E@ W++@ N+++ o K- w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
July 14, 2005
> I still don't see how it relates.
>
>>> Really
>>>
>>>      b &&= c;
>>>
>>> should be equivalent to
>>>
>>>      if (b) b = c;
>>  if (b) -- first access to b
>> b = c -- second access to b
>>  But the operators +=  -=  |=  etc. should evaluate b only once, not twice.
> <snip>
>
> Exactly.  I was using b to refer to the evaluation, rather than the expression.

Hmm, i don't know how to explain. Perhaps "evaluate" is the wrong word? Look into

http://www.digitalmars.com/d/expression.html#AssignExpression

If you have this expression:

b = b + c;

Compiled it will look somehow like this:

make a copy of b
add c to the copy
store the result to b

The other variant:

b += c;

Looks like this when compiled:

add c to b

See the difference? In the first variant b is "evaluated" twice. This is not as important if you use integers, as the resulting machine code will be the same if they are stored on the stack:

mov EAX, b
add EAX, c
mov b, EAX

But if you use user-defined data types, this will be different:

b = b.opAdd(c);
// versus
b.opAddAssign(c);

The second one will often be much faster, as the implementation for opAdd will look like this:

Type opAdd(Type other)
{
  Type copy = *this;
  copy.opAddAssign(other);
  return copy;
}

Ciao
uwe
« First   ‹ Prev
1 2