Safe reference counting cannot be implemented as a library

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 27 05:27:29 PDT 2015


On 10/27/2015 07:57 AM, Manu via Digitalmars-d wrote:
> On 27 October 2015 at 21:41, Andrei Alexandrescu via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
>>
>> It follows that if we want safe reference counting, there must be language
>> support for it. One possibility is to attach an attribute to the class
>> definition:
>>
>> @safe @rc class Widget {
>>    ...
>> }
>
> An attribute? Is presence of opInc()/opDec() insufficient? Would the
> attribute signal fabrication of some default opInc/opDec operators
> applicable for general use?

You're right, opInc/opDec detection would suffice.

Unrelated, and a foreshadowing of the discussion on the lifetime mailing 
list: the compiler has ample opportunity to fuse incs/decs together, so 
the signatures of these functions is:

void opInc(uint delta);
void opDec(uint delta);

For example, consider:

class Widget {
   void fun(Widget, Widget);
}
...
auto w = new Widget;
w.fun(w, w);

In this case the compiler may insert opInc with a value larger than 1 
prior to entering the call.


Andrei



More information about the Digitalmars-d mailing list