www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Structured binding declaration (like in C++)

reply Vindex <tech.vindex gmail.com> writes:
Is there a decomposition for tuples and other data structures?

For example,
```
auto t = tuple(1, "2");
auto (x, y) = t; // or auto (x, y) = t.expand;
```
Oct 13 2021
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/13/21 1:02 PM, Vindex wrote:
 Is there a decomposition for tuples and other data structures?
 
 For example,
 ```
 auto t = tuple(1, "2");
 auto (x, y) = t; // or auto (x, y) = t.expand;
 ```
No, D does not have this (yet?). I thought there was a special case for tuples with foreach but I can't remember it now. (?) Ali
Oct 14 2021
prev sibling parent reply MoonlightSentinel <moonlightsentinel disroot.org> writes:
On Wednesday, 13 October 2021 at 20:02:05 UTC, Vindex wrote:
 Is there a decomposition for tuples and other data structures?
No, but you can emulate it, e.g. by using AliasSeq: ```d import std.meta : AliasSeq; import std.typecons : tuple; import std.stdio : writeln; void main() { int a; string b; AliasSeq!(a, b) = tuple(1, "hello"); writeln("a = ", a); writeln("b = ", b); } ``` https://run.dlang.io/is/aUEtSK
Oct 14 2021
parent Vindex <tech.vindex gmail.com> writes:
On Thursday, 14 October 2021 at 15:29:13 UTC, MoonlightSentinel 
wrote:
 On Wednesday, 13 October 2021 at 20:02:05 UTC, Vindex wrote:
 Is there a decomposition for tuples and other data structures?
No, but you can emulate it, e.g. by using AliasSeq: ```d import std.meta : AliasSeq; import std.typecons : tuple; import std.stdio : writeln; void main() { int a; string b; AliasSeq!(a, b) = tuple(1, "hello"); writeln("a = ", a); writeln("b = ", b); } ``` https://run.dlang.io/is/aUEtSK
Thank you! This is a good variant of decision of the problem
Oct 14 2021