isRangeOf ?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 4 03:59:11 PST 2014


On Tuesday, November 04, 2014 09:40:58 bearophile via Digitalmars-d-learn wrote:
> Sometimes I have a function that needs an iterable:
>
> void foo(Range)(Range data)
> if (isForwardRange!Range && is(Unqual!(ForeachType!Range) ==
> int)) {}
>
>
> So is it a good idea to add a "isRangeOf" template to Phobos?
>
> void foo(Range)(Range data)
> if (isRangeOf!(Range, int)) {}

That loses the ability to test which type of range you're talking about. The
normal thing to do is to simply test the range type and the element type
similar to what you're doing in the first case (though normaly,
Unqual!(ElementType!Range) would be used rather than
Unqual!(ForeachType!Range)). And if what you're really trying to do is check
whether the data variable can be used with foreach, and e in

foreach(e; data)

would be an int, calling it a range isn't really correct anyway, since
opApply and container types would also qualify.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list