Thread overview
Simultaneous Assignment
Dec 09, 2013
Dfr
Dec 09, 2013
qznc
Dec 09, 2013
Dfr
Dec 09, 2013
qznc
Dec 10, 2013
Andrea Fontana
December 09, 2013
Does D has somtething similar ?

http://code.google.com/p/go-wiki/wiki/SimultaneousAssignment

I tried this way, but it not worked out.

if((int x = 10) > 0) {
    writefln("x is %s", x);
}
December 09, 2013
On Monday, 9 December 2013 at 07:38:04 UTC, Dfr wrote:
> Does D has somtething similar ?
>
> http://code.google.com/p/go-wiki/wiki/SimultaneousAssignment

No, not in general. There are a few special cases, though.

The foreach loop can assign value and index simultaneously.

  foreach (int i, char c; a) {
    writefln("a[%d] = '%c'", i, c);
  }

Many things can be done in the library. For example, the variable swap from your link:

  swap(i,j);     // more: http://www.dpaste.dzfl.pl/582f7ae2

For returning multiple values from a function in D, you use std.typecons.tuple.

> I tried this way, but it not worked out.
>
> if((int x = 10) > 0) {
>     writefln("x is %s", x);
> }

Could you give more context for your specific example? What are you trying to do?
December 09, 2013
Sorry, it was misnomer in topic title. But simultaneous assignment also useful topic to learn.

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);
}


On Monday, 9 December 2013 at 08:28:48 UTC, qznc wrote:
>
> Could you give more context for your specific example? What are you trying to do?
December 09, 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.
December 10, 2013
On Monday, 9 December 2013 at 13:57:04 UTC, qznc wrote:
>   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.

Ok then write:

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