how to disable all default N-argument constructors for a struct?
Moritz Maxeiner via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jul 3 18:17:20 PDT 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) 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
More information about the Digitalmars-d
mailing list