Tuple assignment

Oren Tirosh via Digitalmars-d digitalmars-d at puremagic.com
Sun May 10 01:14:50 PDT 2015


On Saturday, 9 May 2015 at 17:16:58 UTC, Russel Winder wrote:
> Python has tuple assignment so you see things like:
>
>         previous, current = current, previous + current
>
> especially if you are doing silly things like calculating 
> Fibonacci
> Sequence values. Translating this to D, you end up with:
>
>         TypeTuple!(current, next) = tuple(next , current +next);

This works right now and is quite aesthetically pleasing:

     tuple(current, next) = tuple(next , current +next);

Note, however, that this is not a tuple assignment. It is 
assignment of a misleadingly-named anonymous struct type that is 
implemented in the standard library.

This is an actual tuple assignment as supported by the compiler:

     a.tupleof = b.tupleof;

I think it should work for any two structs as long their fields 
are public and individually assignment-compatible. I believe it 
is as efficient as individual assignments on all D 
implementations.

A lot of people seem to want better sugar for tuple or tuple-like 
things in D and do not consider std.typecons sufficient (or at 
least find the names it uses confusing). Such a feature would 
work very nicely with D's auto types, return and lambda argument 
type inference, etc. See LINQ.


More information about the Digitalmars-d mailing list