Tuple poilerplate code

JG a at b.com
Tue Sep 1 07:39:30 UTC 2020


On Tuesday, 1 September 2020 at 03:51:10 UTC, user1234 wrote:
> On Tuesday, 1 September 2020 at 02:08:54 UTC, JG wrote:
>> Is there anyway to remove the boilerplate code of dealing with 
>> tuples:
>>
>> I find myself having to write things like this fairly often
>>
>> auto someRandomName  = f(...); //where f returns a tuple with 
>> two parts
>> auto firstPart = someRandomName[0];
>> auto secondPart = someRandomName[1];
>>
>>
>> Is to possible to write something so that the above is 
>> essentially equivalent to:
>>
>> assignTuple!(firstPart,secondPart) = f(...);
>>
>> The closest I can produce is using a template mixin so that I 
>> would have to write:
>>
>> mixin AssignTuple!(()=>f(...),"firstPart","secondPart");
>
> ---
> void assignTuple(S, T...)(auto ref S s, auto ref T t)
> {
>     static foreach (i; 0 .. S.length)
>         t[i] = s[i];
> }
>
> void main()
> {
>     import std;
>     string a,b;
>     tuple("a", "b").assignTuple(a,b);
> }
> ---
Thanks for your answer. That helps somewhat, however it is still 
longer and less clear than one would ideally want. In addition 
you need to use explicit types.



More information about the Digitalmars-d-learn mailing list