digitalmars.D - how to disable all default N-argument constructors for a struct?
- Timothee Cour via Digitalmars-d (12/12) Jul 03 2017 How would I disable the following?
- Stefan Koch (2/14) Jul 03 2017 proving a constructor yourself disables the default.
- Era Scarecrow (9/21) Jul 03 2017 As soon as a class defines at least one constructor, the
- Moritz Maxeiner (3/6) Jul 03 2017 This is also true for structs, but one can't infer that from a
- Moritz Maxeiner (22/29) Jul 03 2017 Disable the struct's default constructor, which implicitly
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 2017
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 fieldsproving a constructor yourself disables the default.
Jul 03 2017
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 fieldsAs 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 2017
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. 182This is also true for structs, but one can't infer that from a rule about classes.
Jul 03 2017
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) argumentsStructs 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 fieldsIf 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 2017