Range question

Jonathan M Davis jmdavisProg at gmx.com
Fri Feb 17 23:01:32 PST 2012


On Friday, February 17, 2012 22:27:06 H. S. Teoh wrote:
> Hmm. But the problem is that I want to be able to handle something like
> File.byLine(). Or perhaps what I really need is just to write a wrapper
> around File.readln() that ensures immutability, then I can use
> isImmutable() to enforce safety in the algorithm, and just pass the
> wrapper when I need to use an underlying File.

If you need to handle arrays differently, then just use a static if to 
specialize the appropriate section of code for them. So, something like

ElementType!R choose(R, E)(R range)
    if(isInputRange!R)
{
    Unqual!(ElementType!R) e;

    foreach(e; range)
    {
        if(/* some criterion */)
        {
            static if(isDynamicArray!(ElementType!R) &&
                      is(Unqual!(ElementType!R) == ElementType!R))
            {
                e = range.front.dup;
            }
            else
                e = range.front;
        }
    }

    return cast(ElementType!R)e;
}

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list