Tuple deconstruction in Phobos
IchorDev
zxinsworld at gmail.com
Sat Jul 20 14:02:21 UTC 2024
Why does Phobos not provide a method to easily deconstruct
tuples? Here's a trivial implementation:
```d
//Similar to C++'s `std::tie`. Can anyone tell me why it's called
`tie`?
void tie(T...)(typeof(T) src){
static foreach(ind, i; src){
T[ind] = i;
}
}
//Usage example:
import std.stdio, std.typecons;
auto tupRetFn() => tuple(47, "test!");
void main(){
string x = "discarded";
int y;
tie!(y, x) = tupRetFn().expand;
writeln(x,", ",y);
}
```
Not having this is like if Phobos didn't have `AliasSeq`. Yes you
can make your own, but it's wasteful boilerplate.
More information about the Digitalmars-d-learn
mailing list