Simultaneous Assignment

qznc qznc at web.de
Mon Dec 9 05:57:02 PST 2013


On Monday, 9 December 2013 at 09:32:26 UTC, Dfr wrote:
> What i trying to achieve in my current example is more succinct 
> code.
> A coming from Perl and instead of writing:
>
> if(some_complex_statement.here > 0) {
>     writefln("x is %s", some_long_expression.here);
> }
>
> I got used not to repeat complex statement, but use 'x' as 
> quick alias:
>
> if((int x = some_complex_statement.here) > 0) {
>     writefln("x is %s", x);
> }

Afaik, D has no StatementExpression, which means no declarations 
inside expressions. However, there is an AssignExpression, so 
assignment works, but requires the declaration before.

   int x;
   if((x = some_long_expression.here) > 0) {
     writefln("x is %s", x);
   }

The bad news is that this means type inference cannot be used 
here (no "auto") and the variables is declared in a wider scope 
than just the if-body.


More information about the Digitalmars-d-learn mailing list