Allow struct constructors with all parameters optional

ryuukk_ ryuukk.dev at gmail.com
Thu Aug 29 13:57:05 UTC 2024


On Wednesday, 28 August 2024 at 15:26:47 UTC, Lance Bachmeier 
wrote:
> On Tuesday, 27 August 2024 at 08:48:19 UTC, Ogi wrote:
>
>> D disallows parameterless constructors for structs, and there 
>> are good reasons for this. But constructors with all 
>> parameters optional are prohibited too. This restriction 
>> doesn’t make much sense, especially now when D supports named 
>> arguments. This should be valid:
>>
>> ```D
>> struct S {
>>     this(int x = 0, int y = 0) {
>>          writeln(i"S($(x), $(y))");
>>     }
>> }
>> auto s1 = S(y:42); // S(0, 42)
>> auto s2 = S(); // default initialization
>> ```
>
> I'll point out that you can already write a library that makes 
> your code run. I'd like a way to do it directly without jumping 
> through these hoops.
>
> foo.d:
>
> ```
> import std;
> struct S {
>     this(int x, int y = 0) {
>          writeln(i"S($(x), $(y))");
>     }
> }
> ```
>
> bar.d:
>
> ```
> import foo;
> alias _S = foo.S;
> _S S() {
>   return _S(0, 0);
> }
> ```
>
> baz.d:
>
> ```
> public import foo, bar;
> alias S = bar.S;
> ```
>
> call.d:
>
> ```
> import baz;
> void main() {
>   auto s2 = S();
> }
> ```

Are you seriously telling people to write a library to be able to 
do what C already provides for decades?

This is why nobody takes D seriously, all that code just to 
initialize a struct

Unbelievable, seriously


More information about the dip.ideas mailing list