isIterable(T)

dsimcha dsimcha at yahoo.com
Sun Apr 26 11:31:33 PDT 2009


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> dsimcha Wrote:
> > I've been thinking a little more about ranges, etc. and it would be nice to
> > have a template for isIterable(T)
> I agree.
> IsIterable is already inside my dlibs (as the xkeys/xvalues you talk about in
another post of yours, but in the "func" module), into the "templates" module:
> http://www.fantascienza.net/leonardo/so/dlibs/templates.html
> I suggest you to take a good look at those dlibs, you will probably find stuff
you are going to think tomorrow and the day after tomorrow.
> Bye,
> bearophile

Thanks.  The other part of the question, which all of us seemed to neglected was
how to get the type of elem.  I just got that to work, too.

import std.traits, std.range;

template IterType(T) {
    alias ReturnType!(
        {
            foreach(elem; T.init) {
                return elem;
            }
        }) IterType;
}

unittest {
    struct Foo {  // For testing opApply.
        // For testing.

        int opApply(int delegate(ref uint) dg) { assert(0); }
    }

    static assert(is(IterType!(uint[]) == uint));
    static assert(is(IterType!(Foo) == uint));
    static assert(is(IterType!(uint[string]) == uint));
    static assert(is(IterType!(Chain!(uint[], uint[])) == uint));
}



More information about the Digitalmars-d mailing list