DIP 1018--The Copy Constructor--Community Review Round 1

Rubn where at is.this
Sat Dec 22 03:37:22 UTC 2018

On Saturday, 22 December 2018 at 00:08:51 UTC, Boris-Barboris 
> On Friday, 21 December 2018 at 21:40:23 UTC, H. S. Teoh wrote:
>> Shouldn't const be inout in this case?
> Just in case: it's not my code, it's from the DIP example 
> snippets.
> I was advocating for forbidden mutable copy source. Inout 
> indeed solves the boilerplate/combinatorial problem, but my 
> main conjecture was about readability, wich is harmed by 
> mutable source default.

The DIP goes over why const wasn't used for the source. Consider 
the following:

struct A {
     int* ptr;

Now to simulate the copy constructor:

A copy(ref const A a) {
     A result;
     result.ptr = a.ptr; // error can't convert `const int*` to 
     return result;

Const is infectious and just causes more problems making it 
pretty much useless in the general case. It's only suitable for a 
very narrow use case. A lot of people using D avoid const 
entirely, except for really basic simple things involving 
primitive types.

More information about the Digitalmars-d mailing list