www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Deduce type of struct in function arguments

reply Andrey <saasecondbox yandex.ru> writes:
Hello,

I have a function and a struct:
 void foo(ref Data data) { ... }
 struct Data
{
    int a;
    string text;
}
How to pass struct into function without naming its type? This doesn't work:
 foo({1234, "Hello!"});
Aug 20 2018
parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 20 August 2018 at 09:23:27 UTC, Andrey wrote:
 Hello,

 I have a function and a struct:
 void foo(ref Data data) { ... }
 struct Data
{
    int a;
    string text;
}
How to pass struct into function without naming its type? This doesn't work:
 foo({1234, "Hello!"});
Create an overload of foo that takes two arguments and combines them into a `Data` struct internally: void foo(int a, string text) { Data data = {a, text}; foo(data); }
Aug 20 2018
parent reply Andrey <saasecondbox yandex.ru> writes:
On Monday, 20 August 2018 at 11:38:39 UTC, Paul Backus wrote:
 Create an overload of foo that takes two arguments and combines 
 them into a `Data` struct internally:

 void foo(int a, string text)
 {
     Data data = {a, text};
     foo(data);
 }
Hmm, not very good solution. In C++ you can not to write type and compiler will deduce it automatically. In D, as I understand, this feature isn't supported.
Aug 20 2018
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Monday, 20 August 2018 at 12:33:34 UTC, Andrey wrote:
 On Monday, 20 August 2018 at 11:38:39 UTC, Paul Backus wrote:
 Create an overload of foo that takes two arguments and 
 combines them into a `Data` struct internally:

 void foo(int a, string text)
 {
     Data data = {a, text};
     foo(data);
 }
Hmm, not very good solution. In C++ you can not to write type and compiler will deduce it automatically. In D, as I understand, this feature isn't supported.
Yes, D's syntax for structure literals requires you to write the name of the type: https://dlang.org/spec/struct.html#struct-literal If the name of the type is too cumbersome to write out, you can use an alias: alias ShortName = MyLongAndComplicatedType!(PossiblyWith, TemplateArguments); foo(ShortName(a, text));
Aug 20 2018
prev sibling parent reply Seb <seb wilzba.ch> writes:
On Monday, 20 August 2018 at 12:33:34 UTC, Andrey wrote:
 On Monday, 20 August 2018 at 11:38:39 UTC, Paul Backus wrote:
 Create an overload of foo that takes two arguments and 
 combines them into a `Data` struct internally:

 void foo(int a, string text)
 {
     Data data = {a, text};
     foo(data);
 }
Hmm, not very good solution. In C++ you can not to write type and compiler will deduce it automatically. In D, as I understand, this feature isn't supported.
... yet. Though you can vote for this DIP and show your support there: https://github.com/dlang/DIPs/pull/71 It even comes with an implementation in DMD already: https://github.com/dlang/dmd/pull/8460
Aug 20 2018
parent reply Andrey <saasecondbox yandex.ru> writes:
On Monday, 20 August 2018 at 17:45:25 UTC, Seb wrote:
 ... yet. Though you can vote for this DIP and show your support 
 there:

 https://github.com/dlang/DIPs/pull/71

 It even comes with an implementation in DMD already:

 https://github.com/dlang/dmd/pull/8460
How and where to vote?
Aug 20 2018
parent reply Seb <seb wilzba.ch> writes:
On Tuesday, 21 August 2018 at 06:29:12 UTC, Andrey wrote:
 On Monday, 20 August 2018 at 17:45:25 UTC, Seb wrote:
 ... yet. Though you can vote for this DIP and show your 
 support there:

 https://github.com/dlang/DIPs/pull/71

 It even comes with an implementation in DMD already:

 https://github.com/dlang/dmd/pull/8460
How and where to vote?
At the pull request via +1 and the upcoming "official" discussion of the PR. The last discussion was here (https://forum.dlang.org/post/lpixarbirhorkltaqlew forum.dlang.org) and it seems there's no clear consensus on the best syntax for this yet :/
Aug 21 2018
parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 21 August 2018 at 08:32:17 UTC, Seb wrote:

 How and where to vote?
At the pull request via +1 and the upcoming "official" discussion of the PR.
Except that there's no voting process for DIPs. The Community Review is not intended as a way to vote, simply to provide feedback on it with the aim of making it a better proposal. I know people tend to throw their +1's in there, but I wouldn't expect Walter and Andrei to put any weight on that.
Aug 21 2018
parent reply Seb <seb wilzba.ch> writes:
On Tuesday, 21 August 2018 at 08:47:53 UTC, Mike Parker wrote:
 On Tuesday, 21 August 2018 at 08:32:17 UTC, Seb wrote:

 How and where to vote?
At the pull request via +1 and the upcoming "official" discussion of the PR.
Except that there's no voting process for DIPs. The Community Review is not intended as a way to vote, simply to provide feedback on it with the aim of making it a better proposal. I know people tend to throw their +1's in there, but I wouldn't expect Walter and Andrei to put any weight on that.
I'm aware, but we don't have any other process for people to show their support for a DIP, do we? And for DMD/Druntime/Phobos etc. having a bunch of +1 helps a PR to move faster as we as reviewers can thus realize that this is sth. that's very important to the community.
Aug 21 2018
parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 21 August 2018 at 14:56:21 UTC, Seb wrote:
 I'm aware, but we don't have any other process for people to 
 show their support for a DIP, do we?


 And for DMD/Druntime/Phobos etc. having a bunch of +1 helps a 
 PR to move faster as we as reviewers can thus realize that this 
 is sth. that's very important to the community.
Sure, I know you know. I didn't want anyone reading what you wrote to get the impression that DIP reviews are an actual vote where the +1s are tallied up.
Aug 21 2018
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Tuesday, August 21, 2018 6:56:06 PM MDT Mike Parker via Digitalmars-d-
learn wrote:
 On Tuesday, 21 August 2018 at 14:56:21 UTC, Seb wrote:
 I'm aware, but we don't have any other process for people to
 show their support for a DIP, do we?


 And for DMD/Druntime/Phobos etc. having a bunch of +1 helps a
 PR to move faster as we as reviewers can thus realize that this
 is sth. that's very important to the community.
Sure, I know you know. I didn't want anyone reading what you wrote to get the impression that DIP reviews are an actual vote where the +1s are tallied up.
It would probably be better to tell people to show their appreciation or support for the DIP by adding +1 to it than it would be to say anything about voting, since no voting is involved, and this is not a democratic process in any way shape or form. LOL. It's much more like a petition to the king. :) - Jonathan M Davis
Aug 21 2018