Reference counted containers prototype

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Dec 27 07:04:54 PST 2011


On 12/27/11 5:32 AM, deadalnix wrote:
> The first thing that I see looking at the source code is how many null
> check you'll find in RefCounted . As the RefCounted struct is useless
> without a payload, we should ensure that the payload exists, in all
> cases, and not null check at every operation.

D's take on default constructor makes it impossible to initialize the 
object 100% automatically. What can be done is a test inside opDispatch 
followed by default construction if the object is null. That's not 
terribly useful and intuitive. Consider:

void fun(DList!int lst)
{
    lst.insertFront(42);
}

void main()
{
     DList!int lst1;
     lst1.insertFront(43);
     fun(lst1);
     assert(lst1.front == 42); // fine

     DList!int lst2;
     fun(lst2);
     assert(lst2.front == 42); // gaaaaaaa!
}

This is the behavior of any reference type that can be null, because 
null doesn't refer to any object. IMHO this, and not null pointer 
exceptions, is the largest liability of null.


Andrei


More information about the Digitalmars-d mailing list