Allow struct constructors with all parameters optional
Lance Bachmeier
no at spam.net
Wed Aug 28 15:26:47 UTC 2024
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();
}
```
More information about the dip.ideas
mailing list