Choice ranges?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Mar 28 15:10:28 PDT 2014


On Fri, Mar 28, 2014 at 10:44:11PM +0100, Artur Skawina wrote:
> On 03/28/14 20:00, H. S. Teoh wrote:
> > Today I ran into an interesting situation where I have a function f
> > that needs to return ranges of different types (but identical
> > element types):
> > 
> > 	auto f(A...)(A args) {
> > 		...
> > 		if (someCondition)
> > 			return cartesianProduct(x, y)
> > 				.joiner;
> > 		else
> > 			return cartesianProduct(x, y)
> > 				.joiner
> > 				.filter!someFilter;
> > 	}
> > 
> > This obviously can't compile, because the return types are not the
> > same.  (Note that someCondition is only known at runtime.) But
> > abstractly speaking, it *should* work, because the element type of
> > the returned range is identical.
> > 
> > So how would I implement something like this?
> 
> eg
>  	auto f(A...)(A args) {
>  		...
>  		return cartesianProduct(x, y)
>  			.joiner
>  			.filter!(a=>somecondition?true:somefilter(a));
>  	}
[...]

It's not that simple, though.  I guess I gave a bad example. In the
actual code, someCondition decides whether or not a cartesian product is
even used. So the return range needs to somehow switch between a
cartesianProduct filtered by some filters, vs. an alternative algorithm
filtered by some other filters.


T

-- 
Famous last words: I *think* this will work...


More information about the Digitalmars-d-learn mailing list