Proposal : allocations made easier with non nullable types.

Nick Sabalausky a at a.a
Tue Feb 10 10:30:10 PST 2009


"Andrei Alexandrescu" <SeeWebsiteForEmail at erdani.org> wrote in message 
news:gmsd6o$2h77$1 at digitalmars.com...
>
> 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.
>

------------------
class LinkedListNode(T)
{
    static this() {
         _end = new LinkedListNode!(T)(55555);
    }

    this() {
        // Prevent _next from being auto-inited to
        // a new LinkedListNode, which in turn
        // would have a _next auto-inited to a
        // new LinkedListNode, etc.
        _next = _end;
    }

    // Prevent _end._next from being auto-inited to
    // a new LinkedListNode, which in turn
    // would have a _next auto-inited to a
    // new LinkedListNode, etc.
    private this(int dummy) {
        _next = this;
    }

    private static LinkedListNode!(T) _end;
    static LinkedListNode!(T) end() {
        return _end;
    }

    private LinkedListNode!(T) _next;
    void next(LinkedListNode!(T) newNext) {
        _next = newNext;
    }
    LinkedListNode!(T) next() {
        // Prevent infinite iteration
        if(this is _end)
            throw new Exception("Iterated past the end!");
        else
            return _next;
    }
}
------------------

Note, however, that I'm not advocating this approach to linked lists or 
other advanced structures (since it's so much uglier than just using a 
nullable reference). I'm just pointing out that it's at least *possible* 
without using nullable references.





More information about the Digitalmars-d mailing list