digitalmars.D.learn - isRangeOf ?
- bearophile (9/9) Nov 04 2014 Sometimes I have a function that needs an iterable:
- Jonathan M Davis via Digitalmars-d-learn (11/18) Nov 04 2014 That loses the ability to test which type of range you're talking about....
- bearophile (14/16) Nov 04 2014 Yes, one can think about templates like "isForwardRangeOf", etc.
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)) {} Bye, bearophile
Nov 04 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
Nov 04 2014
Jonathan M Davis:That loses the ability to test which type of range you're talking about.Yes, one can think about templates like "isForwardRangeOf", etc. But for such specialized cases I think using isForwardRange+is(ElementType) is acceptable and avoids adding too many templates to Phobos. isRangeOf is for the common case where any range is OK (a forward range), and you need a range of items of type T (opApply still gives an iterable but unfortunately it's not compatible with most Phobos. That's why I have not suggested a function named "isIterableOf" meant to accept opApply too). I have opened an ER: https://issues.dlang.org/show_bug.cgi?id=13682 Bye, bearophile
Nov 04 2014