Go, D, and the GC

Shachar Shemesh via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 5 03:57:57 PDT 2015


On 05/10/15 13:39, Marc Schütz wrote:
> On Monday, 5 October 2015 at 09:25:30 UTC, Shachar Shemesh wrote:
>> What's more, init is used even if you @disable this(). The following
>> compile and does what you'd expect (but not what you want):
>> struct S {
>>    int d;
>>
>>    @disable this();
>>    this( int d ) {
>>       this.d = d;
>>    }
>> }
>>
>>
>> ...
>>
>>    S d = S.init;
>
> I don't understand this. How is that not what you want, considering that
> you explicitly told it to use the init value?

One of the basics of C++ is that, if you construct the type correctly, 
then the user of the type cannot use it in a way that will cause 
inconsistencies. Such a use will not compile.

The above shows that you cannot construct such a type in D. The language 
simply does not allow you to cancel a certain feature of the type in 
which you are uninterested.

Please bear in mind that the init might not be called directly. 
Programmer A defines a type that should never be uninitialized (i.e. - 
needs a constructor). Programmer B places an instance of that type 
inside a containing struct. Programmer C uses that containing struct's 
init. Such a problem is not easy to catch, even if programmers A, B and 
C are the same person.

Shachar


More information about the Digitalmars-d mailing list