Jump to page: 1 2
Thread overview
Deduce type of struct in function arguments
Aug 20, 2018
Andrey
Aug 20, 2018
Paul Backus
Aug 20, 2018
Andrey
Aug 20, 2018
Paul Backus
Aug 20, 2018
Seb
Aug 21, 2018
Andrey
Aug 21, 2018
Seb
Aug 21, 2018
Mike Parker
Aug 21, 2018
Seb
Aug 22, 2018
Mike Parker
Aug 22, 2018
Jonathan M Davis
August 20, 2018
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!"});
August 20, 2018
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);
}
August 20, 2018
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.
August 20, 2018
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));
August 20, 2018
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
August 21, 2018
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?
August 21, 2018
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 :/
August 21, 2018
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.
August 21, 2018
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.
August 22, 2018
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.
« First   ‹ Prev
1 2