Partial return type specification
bearophile
bearophileHUGS at lycos.com
Mon Oct 4 10:00:24 PDT 2010
Pelle:
> You were probably looking for a more general and, well, good, solution.
> This does however convey some intent, and kind of works.
Your solution looks cute enough, I may even try to use it. But a template seems better than a function template (because eventually functions are supposed to require the () to be called):
import std.algorithm: map, equal;
import std.traits: ForeachType, Unqual;
template AssertReturnsRangeOf(T) {
enum string AssertReturnsRangeOf =
"static assert (is(Unqual!(ForeachType!(typeof(return))) == "
~ T.stringof ~ "));";
}
auto foo() {
return map!q{a*a}([1,2,3,4]);
mixin(AssertReturnsRangeOf!int);
}
void main() {
assert(equal(foo(), [1, 4, 9, 16]));
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list