Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
July 03, 2017 how to disable all default N-argument constructors for a struct? | ||||
---|---|---|---|---|
| ||||
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 |
July 04, 2017 Re: how to disable all default N-argument constructors for a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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.
|
July 04, 2017 Re: how to disable all default N-argument constructors for a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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
|
July 04, 2017 Re: how to disable all default N-argument constructors for a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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 |
July 04, 2017 Re: how to disable all default N-argument constructors for a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow | 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.
|
Copyright © 1999-2021 by the D Language Foundation