On Sun, 28 Nov 2010 06:19:00 -0500
bearophile <bearophileh...@lycos.com> wrote:

> Tom:
> 
> > What should I do? Is there some way to get the original array type?
> [...]
> void foo(Range)(Range srange) if (is(ForeachType!Range == string)) {
>      writeln(srange);
> }
> [...]
> If you want to simplify your code a little you may define a helper template 
> like this:
> 
> template IsIterableType(Range, T) {
>     enum bool IsIterableType = is(ForeachType!Range == T);
> }
> 
> void foo2(Range)(Range srange) if (IsIterableType!(Range, string)) {
>      writeln(srange);
> }

I was just thinking that all the complication about is() (or un-intuitiveness) 
may be replaced by a proper use of interfaces. Possibly with a simple operator 
like 'isa'. Maybe:
void foo2(Range)(Range srange) if (Range isa Iterable(string)) {
     writeln(srange);
}
or in this case
void foo2(Range!T)(Range!T srange) if (T == string && Range isa Iterable) {
     writeln(srange);
}
...which is a bit strange because Range exists precisely to be iterable (so 
that "Range isa Iterable" is nearly synonym of "Range isa Range"), so this may 
reduce to
void foo2(Range!T)(Range!T srange) if (T == string) {
     writeln(srange);
}

(Sure, we may find better idioms.)
After all, isn't the whole point of interfaces to tell that a given type has a 
given capability?

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

Reply via email to