Map and arrays

spir denis.spir at gmail.com
Sun Nov 28 09:43:11 PST 2010


On Sun, 28 Nov 2010 06:19:00 -0500
bearophile <bearophileHUGS at 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



More information about the Digitalmars-d-learn mailing list