Choice ranges?

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Mar 29 20:35:13 PDT 2014


> On 03/28/2014 08:00 PM, 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?
[...]

Well, eventually I settled on using inputRangeObject() from std.range.
While not the most performant (f would have to return InputRangeObject
interface, which adds indirection), it was simplest to use and didn't
require excessively ugly code. If this part of the code turns out to be
a bottleneck, I'll rethink this decision, but for now it works nicely.
:)


T

-- 
The easy way is the wrong way, and the hard way is the stupid way. Pick one.


More information about the Digitalmars-d-learn mailing list