Simultaneous Assignment

qznc qznc at web.de
Mon Dec 9 00:28:46 PST 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?


More information about the Digitalmars-d-learn mailing list