RFC on range design for D2

superdan super at dan.org
Tue Sep 9 07:01:32 PDT 2008


Andrei Alexandrescu Wrote:

> What we want is a design that tells the truth. And a design that tells
> the truth is this:
> 
> r.isEmpty does whatever the hell it takes to make sure whether there's
> data available or not. It is not const and it could throw an exception.
> 
> v = r.getNext returns BY VALUE by means of DESTRUCTIVE COPY data that
> came through the wire, data that the client now owns as soon as getNext
> returned. There is no extra copy, no extra allocation, and the real
> thing has happened: data has been read from the outside and user code
> was made the only owner of it.

this is really kool n the gang. there's a sore point tho. if i wanna read strings from a file no prob.

for (auto r = stringSucker(stdin); !r.isEmpty(); )
{
    string s = r.getNext();
    // play with s
}

but a new string is allocated every line. that's safe but slow. so i want some more efficient stuff. i should use char[] because string don't deallocate.

for (auto r = charArraySucker(stdin); !r.isEmpty(); )
{
    char[] s = r.getNext();
    // play with s
}

no improvement. same thing a new char[] is alloc each time. maybe i could do

for (auto r = charArraySucker(stdin); !r.isEmpty(); )
{
    char[] s = r.getNext();
    // play with s
    delete s;
}

would this make stuff faster. maybe. maybe not. and it's not general. what i'd like is some way of telling the range, i'm done with this you can recycle and reuse it. it's a green green world.

for (auto r = charArraySucker(stdin); !r.isEmpty(); )
{
    char[] s = r.getNext();
    // play with s
    r.recycle(s);
}

sig is recycle(ref ElementType!(R)).


More information about the Digitalmars-d-announce mailing list