www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to gets multi results using tuple in D?

reply zoujiaqing <zoujiaqing gmail.com> writes:
C++ Code:
```cpp
std::tuple<bool, int, int, std::string_view> DoIt()
{
     return {false, 0, 0, "Hello"};
}

auto [r1, r2, r3, r4] = DoIt();

if (r1 == false)

```


D Code:

```D
Tuple!(bool, int, int, string) DoIt()
{
     return [false, 1, 1, "Hello"];
}

auto result = DoIt();

auto r1= result[0];
auto r2= result[1];
auto r3= result[2];
auto r3= result[3];

if (r1 == false)
```

D requires more lines of code.
Dec 23 2021
next sibling parent reply WebFreak001 <d.forum webfreak.org> writes:
On Thursday, 23 December 2021 at 08:33:17 UTC, zoujiaqing wrote:
 C++ Code:
 ```cpp
 std::tuple<bool, int, int, std::string_view> DoIt()
 {
     return {false, 0, 0, "Hello"};
 }

 auto [r1, r2, r3, r4] = DoIt();

 if (r1 == false)

 ```


 D Code:

 ```D
 Tuple!(bool, int, int, string) DoIt()
 {
     return [false, 1, 1, "Hello"];
 }

 auto result = DoIt();

 auto r1= result[0];
 auto r2= result[1];
 auto r3= result[2];
 auto r3= result[3];

 if (r1 == false)
 ```

 D requires more lines of code.
I think this is the best thing you can do: ```d bool r1; int r2, r3; string r4; AliasSeq!(r1, r2, r3, r4) = DoIt(); ``` https://forum.dlang.org/thread/kmugmwmduxeoyfffoiif forum.dlang.org
Dec 23 2021
next sibling parent russhy <russhy_s gmail.com> writes:
it's one of those things where D is starting to fall behind 
competition when it comes to quality of life features

while OP's example is not that bad, the AliasSeq is plain and 
simple just confusing.. it's a NO

also removing the need of import for tuple is needed!!


it's the same with sumtype/tagged unions and enums literals


we need a workgroup to tackle these issues quick

some new languages are building momentum, it'll  be hard to 
compete when they get released...

we better get ready now and maintain a good balance of new 
features without forgetting about quality of life

and deprecating/removing old features that holds us back
Dec 23 2021
prev sibling parent zoujiaqing <zoujiaqing gmail.com> writes:
On Thursday, 23 December 2021 at 08:56:36 UTC, WebFreak001 wrote:
 On Thursday, 23 December 2021 at 08:33:17 UTC, zoujiaqing wrote:
 C++ Code:
 ```cpp
 std::tuple<bool, int, int, std::string_view> DoIt()
 {
     return {false, 0, 0, "Hello"};
 }

 auto [r1, r2, r3, r4] = DoIt();

 if (r1 == false)

 ```


 D Code:

 ```D
 Tuple!(bool, int, int, string) DoIt()
 {
     return [false, 1, 1, "Hello"];
 }

 auto result = DoIt();

 auto r1= result[0];
 auto r2= result[1];
 auto r3= result[2];
 auto r3= result[3];

 if (r1 == false)
 ```

 D requires more lines of code.
I think this is the best thing you can do: ```d bool r1; int r2, r3; string r4; AliasSeq!(r1, r2, r3, r4) = DoIt(); ``` https://forum.dlang.org/thread/kmugmwmduxeoyfffoiif forum.dlang.org
Thanks! It's not concise!
Dec 26 2021
prev sibling parent zjh <fqbqrr 163.com> writes:
On Thursday, 23 December 2021 at 08:33:17 UTC, zoujiaqing wrote:
 C++ Code:
also `...`,it's very useful.
Dec 23 2021