www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [OT] Assignment expression as RValue

reply Derek Parnell <derek psych.ward> writes:
Hi,
I'm having a bit of a discussion with some colleagues about assignments.
I'm trying to come up with a example (or two) in which the use of an
assignment expression as an RValue is the only (best?) way to do it.

  eg..
   someOperation(X = SomeExpression);

So far, it seems that one can always do ...

   X = SomeExpression;
   someOperation(X);

and a half decent optimizer will generate the same code.

Even ... 

   A = B = C = D;

can be expressed as ...

   C = D;
   B = D;
   A = D;

So can anyone help me with an example, or is such a beastie just a syntax
shorthand?


-- 
Derek
Melbourne, Australia
10/Aug/04 1:57:47 PM
Aug 09 2004
parent Andy Friesen <andy ikagames.com> writes:
Derek Parnell wrote:
 Hi,
 I'm having a bit of a discussion with some colleagues about assignments.
 I'm trying to come up with a example (or two) in which the use of an
 assignment expression as an RValue is the only (best?) way to do it.
 
   eg..
    someOperation(X = SomeExpression);
 
 So far, it seems that one can always do ...
 
    X = SomeExpression;
    someOperation(X);
 
 and a half decent optimizer will generate the same code.
 
 Even ... 
 
    A = B = C = D;
 
 can be expressed as ...
 
    C = D;
    B = D;
    A = D;
 
 So can anyone help me with an example, or is such a beastie just a syntax
 shorthand?

I prefer for local variables to exist only where they are used, so it's useful to be able to write something like this: if (DerivedClass* derivedPtr = dynamic_cast<DerivedClass>(some_base)) { ... } The local 'derivedPtr' exists only within the if block, where it belongs. The only other way to do this is to create a naked block just for the if statement. It works, but it's much uglier looking and involves another level of nesting. (opinions may vary) -- andy
Aug 09 2004