Partial return type specification

bearophile bearophileHUGS at lycos.com
Mon Oct 11 13:56:15 PDT 2010


Pelle:

> string assertReturnsRangeOf(T)() {
>      return "static assert (is(Unqual!(ForeachType!(typeof(return))) == "
>          ~ T.stringof ~ "));";
> }
> 
> auto foo() {
>      return map!q{a*a}([1,2,3,4,5]);
>      mixin (assertReturnsRangeOf!int);
> }
> 
> You were probably looking for a more general and, well, good, solution. 
> This does however convey some intent, and kind of works.

Ideally the partial specification syntax for ranges may work at the calling point too:

void main() {
    Range!int results = foo();
}

Here results is of its specific type, it's not a "Range!int", so Range!int works as "auto". The difference is that the compiler makes sure that's an iterable of ints, and not an iterable of floats or a not iterable, etc.

Bye,
bearophile


More information about the Digitalmars-d mailing list