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...

Reply via email to