Private struct constructor

Simen Kjærås simen.kjaras at gmail.com
Thu Oct 4 08:14:44 UTC 2018


On Thursday, 4 October 2018 at 07:31:21 UTC, Ritchie wrote:
> Any reason why this works?
>
> https://run.dlang.io/is/TALlyw

Yup.


Alright, so there's a few features in use here - which one are 
you asking about?

1. Private constructor.
You can call the private constructor because the unit of 
encapsulation in D is the module, not the type. So everything is 
visible to everything else inside the same module.

2. @disable this()
This only disables default construction. Since you call a 
different constructor, this doesn't affect the compilation.

3. @disable this(this)
This disables copy construction. In the example, no copying 
occurs - the variable declaration leads to a move instead. Simply 
put, copy construction requires that two copies exist 
simultaneously, and in this case the potential second copy is 
immediately destroyed, so no copy is necessary.

4. @disable void opAssign()
There's several reasons this doesn't affect compilation here. 
First, the signature is wrong - you're disabling an opAssign that 
takes no arguments. You're probably wanting to do @disable void 
opAssign(X x);.

Now, even that isn't going to cause it to fail to compile in this 
case though, since as in #3, what's happening on line 14 is move 
construction, not assignment. To force an assignment, you need to 
have an object already:

X x = void;
x = X(5); // calls opAssign

So, now we've explained why it compiles. Currently, there's no 
way to @disable or hook move construction. There's a DIP in the 
works (DIP 1014) that aims to provide a hook into move 
construction, but I don't see that it allows for @disabling it 
altogether.

Hope this helps!

--
   Simen


More information about the Digitalmars-d-learn mailing list