How to unpack a tuple into multiple variables?

Gary Chike chikega at gmail.com
Mon Feb 5 21:12:58 UTC 2024


I hope all is well with everyone. I have come to an impasse. What 
is the best way to unpack a tuple into multiple variables in D 
similar to this Python code? Thank you!

```python
# Example tuple
my_tuple = (2010, 10, 2, 11, 4, 0, 2, 41, 0)

# Unpack the tuple into separate variables
year, month, day, hour, minute, second, _, _, _ = my_tuple

print(f"Year: {year}, Month: {month}, Day: {day}")
print(f"Time: {hour}:{minute}:{second}")
```
My simple D code:

```d
module tupleUnpack2;

import std.stdio;
import std.typecons : tuple;

void main() {
     auto fruits = tuple("apple", "banana", "cherry");

     writeln(fruits[0]); // "apple"
     writeln(fruits[1]); // "banana"
     writeln(fruits[2]); // "cherry"

     auto (a, b, c) = fruits; // wish it were this easy (C#-like 
syntax)
     //or
     auto(a, b ,c) = fruits.expand;
}
```



More information about the Digitalmars-d-learn mailing list