Proposal : allocations made easier with non nullable types.

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Feb 10 09:23:37 PST 2009


Denis Koroskin wrote:
> On Tue, 10 Feb 2009 19:41:25 +0300, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> Denis Koroskin wrote:
>>> On Tue, 10 Feb 2009 17:40:36 +0300, Andrei Alexandrescu 
>>> <SeeWebsiteForEmail at erdani.org> wrote:
>>>
>>>> Denis Koroskin wrote:
>>>>> On Tue, 10 Feb 2009 17:11:06 +0300, Andrei Alexandrescu 
>>>>> <SeeWebsiteForEmail at erdani.org> wrote:
>>>>>
>>>>>> Nick Sabalausky wrote:
>>>>>>> "Daniel Keep" <daniel.keep.lists at gmail.com> wrote in message 
>>>>>>> news:gmpd71$8uj$1 at digitalmars.com...
>>>>>>>> Alex Burton wrote:
>>>>>>>>> I think it makes no sense to have nullable pointers in a high 
>>>>>>>>> level language like D.
>>>>>>>> Oh, and how do you intend to make linked lists?  Or trees?  Or any
>>>>>>>> non-trivial data structure?
>>>>>>>>
>>>>>>>  Null Object Pattern:
>>>>>>> --------------
>>>>>>> class LinkedListNode(T)
>>>>>>> {
>>>>>>>     LinkedListNode!(T) next;
>>>>>>>      private static LinkedListNode!(T) _end;
>>>>>>>     static LinkedListNode!(T) end() {
>>>>>>>         return _end;
>>>>>>>     }
>>>>>>>     static this() {
>>>>>>>          _end = new LinkedListNode!(T);
>>>>>>>     }
>>>>>>> }
>>>>>>> --------------
>>>>>>
>>>>>>
>>>>>> What is "next" for the _end node?
>>>>>>
>>>>>> Andrei
>>>>>  Perhaps, 'this' (i.e. itself).
>>>>
>>>> Then there's going to be quite a few infinite loops out there...
>>>>
>>>> Andrei
>>>  No, you just need to have a private ctor:
>>
>> I meant that code that fails to check for _end when iterating through 
>> a list will never stop.
>>
>> Andrei
> 
> No, error like that will be detected at compile time (comparison against 
> null is disallowed for non-nullable reference).

I must be misunderstanding something. At the highest level, the example 
given replaced the terminating node of a list (which traditionally is 
null) with a node that had itself as the next node.

This means that if you iterate such a list without testing for node == 
_end, you will have an infinite loop. This has nothing to do with types.


Andrei



More information about the Digitalmars-d mailing list