AliasTuples, rather than Records, to return multiple values

Dario Schiavon dario.schiavon at gmail.com
Fri May 18 04:00:28 PDT 2012


On Friday, 18 May 2012 at 10:47:11 UTC, Dario Schiavon wrote:
> On Thursday, 17 May 2012 at 17:13:50 UTC, deadalnix wrote:
>> [...]
>>
>> I think you show a real need here, but I don't really like 
>> your proposal. I'd advocate for recycling the comma operator 
>> for tuple building.
>>
>> This would be very similar to your proposal as a result, but 
>> no need to introduce a new syntax.
>
> As I see it, creating single-item tuples would still be 
> difficult with the comma syntax, except by introducing syntaxes 
> like (a,) or (a,void), which don't look very good. Sure, 
> single-item tuples don't appear that useful at first, but let's 
> assume you want to append a new item to the tuple: can you 
> write anything better than tuple~(a,)? Python's solution is not 
> very elegant in these cases.

Although, it just came to my mind, appending new items to tuples 
is quite easy with the actual rules for AliasTuples. Since 
AliasTuples can't have a hierarchy, they always unpack 
automatically. So the expression "tuple,a" would actually append 
the item "a" to the AliasTuple "tuple". The following works 
already in D:

import std.stdio;
import std.typetuple;

void main() {
   alias TypeTuple!(1, 2) a;
   alias TypeTuple!(a, 3) b;  // appends 3 to a
   writeln(b);  // prints "123"
}

Appending items to tuples actually covers 99% of my needs of 
single-item tuples in Python. Can anyone find other needs for 
single-item tuples? Or for empty tuples?

>
> However, another point of my post was whether we really need 
> Records (std.typecons.Tuple's), which take approximately the 
> same role as traditional struct's after all, to allow returning 
> multiple values from functions. Wouldn't AliasTuples take on 
> the role as well?


More information about the Digitalmars-d mailing list