Deduce type of struct in function arguments

Paul Backus snarwin at gmail.com
Mon Aug 20 13:20:18 UTC 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));


More information about the Digitalmars-d-learn mailing list