www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - how to disable all default N-argument constructors for a struct?

reply Timothee Cour via Digitalmars-d <digitalmars-d puremagic.com> writes:
How would I disable the following?

```
auto a1=A(1);
auto a2=A(1, "b");

struct A{
  int a;
  string b;
  //  disable default constructors with N(N>=1) arguments
}
```

I'd like to force the user to set fields explicitly, so as to make it
more safe to add / move fields
Jul 03
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 4 July 2017 at 00:46:36 UTC, Timothee Cour wrote:
 How would I disable the following?

 ```
 auto a1=A(1);
 auto a2=A(1, "b");

 struct A{
   int a;
   string b;
   //  disable default constructors with N(N>=1) arguments
 }
 ```

 I'd like to force the user to set fields explicitly, so as to 
 make it more safe to add / move fields
proving a constructor yourself disables the default.
Jul 03
prev sibling next sibling parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
On Tuesday, 4 July 2017 at 00:46:36 UTC, Timothee Cour wrote:
 How would I disable the following?

 ```
 auto a1=A(1);
 auto a2=A(1, "b");

 struct A{
   int a;
   string b;
   //  disable default constructors with N(N>=1) arguments
 }
 ```

 I'd like to force the user to set fields explicitly, so as to 
 make it more safe to add / move fields
As soon as a class defines at least one constructor, the implicit default constructor is not avaliable anymore. - TDPL pg. 182 So to answer your question, define a constructor. perhaps: this(int _a, string_b) {a=_a; b=_b;} then: auto a1=A(1); //fails auto a2=A(1, "b"); //passes
Jul 03
parent Moritz Maxeiner <moritz ucworks.org> writes:
On Tuesday, 4 July 2017 at 01:02:16 UTC, Era Scarecrow wrote:
  As soon as a class defines at least one constructor, the 
 implicit default constructor is not avaliable anymore. - TDPL 
 pg. 182
This is also true for structs, but one can't infer that from a rule about classes.
Jul 03
prev sibling parent Moritz Maxeiner <moritz ucworks.org> writes:
On Tuesday, 4 July 2017 at 00:46:36 UTC, Timothee Cour wrote:
 How would I disable the following?

 ```
 auto a1=A(1);
 auto a2=A(1, "b");
Disable the struct's default constructor, which implicitly disables struct literals for it [1]. --- struct A { int a; string b; disable this(); // Disable the default constructor -> implicitly disables struct literals } ---
 //  disable default constructors with N(N>=1) arguments
Structs have only a single default constructors (with zero arguments) - which you can disable (as shown above); you were using struct literals [2].
 I'd like to force the user to set fields explicitly, so as to 
 make it more safe to add / move fields
If I understand you correctly, that is what you get when you disable the default constructor (again, there is only the zero arguments one). You will have to initialize your structs, explicitly, then, though, i.e. `A a = A.init`. [1] https://dpaste.dzfl.pl/b7eef8518799 [2] https://dlang.org/spec/struct.html#StructLiteral
Jul 03