struct parameterless constructor

Alex Bryan abryancs at gmail.com
Mon Aug 4 23:03:56 UTC 2025


On Monday, 4 August 2025 at 20:02:48 UTC, Brother Bill wrote:
> I feel like I am going into the hornet's nest with this 
> discussion.
>
> I have created a struct with some members, and want to have a 
> parameterless constructor that sets the member values at run 
> time.
>
> I have seen things like @disable this();  and static opCall(), 
> but don't quite understand them.
>
> How does one do this, and what are the ramifications of doing 
> so?
> Or should one just leave the parameterless constructor as is?

I'm not 100% sure why parameterless constructors are banned on 
structs, but you can work around it by making a static method:

```
struct Foo {
     int a;
     float b;

     static Foo make() {
         // make "default" version of struct
         return Foo(1, 2.0);
     }
}

void main() {
     Foo f = Foo.make();
}
```


More information about the Digitalmars-d-learn mailing list