Verbosity in D

Emanuele Torre torreemanuele6 at gmail.com
Sun Aug 7 23:44:26 UTC 2022


On Sunday, 7 August 2022 at 23:31:45 UTC, pascal111 wrote:
> On Sunday, 7 August 2022 at 22:16:55 UTC, Emanuele Torre wrote:
>> On Sunday, 7 August 2022 at 20:15:05 UTC, pascal111 wrote:
>>> What destructuring binds? I didn't hear about that before.
>>
>> ```C++
>> #include <iostream>
>>
>> struct Point {
>>     int x, y;
>> };
>>
>> Point add_points(const Point& a, const Point& b)
>> {
>>     return { a.x + b.x, a.y + b.y };
>> }
>>
>> int main()
>> {
>>     const auto [x, y] = add_points({ 1, 10 }, { -60, 40 });
>>     std::cout << "x is: " << x << '\n';
>>     std::cout << "y is: " << y << '\n';
>> }
>> ```
>
> It seems complex, I didn't get it yet, I wished I didn't ask 
> about it :)

It's really trivial.
`auto [x, y] = getpoint();` is equivalent to
```C++
auto _p = getpoint();
auto x = _p.x /* first element of the struct */;
auto y = _p.y /* second element of the struct */;`
```
Also works with arrays.
```C++
int[] arr = { 10, 12, 14 };
const auto [x, y, z] = arr;
/* x=10 y=12 z=14 */
```
A lot of programming languages have this.


More information about the Digitalmars-d-learn mailing list