Tuple poilerplate code

user1234 user1234 at 12.de
Tue Sep 1 03:51:10 UTC 2020


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


More information about the Digitalmars-d-learn mailing list