Andrej Mitrovic:

it just occurred to me that you can actually use a static assert on a return type in D, e.g.:

import std.traits;
import std.algorithm;

auto min(T1, T2)(T1 t1, T2 t2)
{
    return t1;  // e.g. implementation bug
    static assert(is(typeof(return) == CommonType!(T1, T2)));
}

void main() {
    auto x = min(1, 1.0);
}

I didn't know this until now.

I'd like to verify the type of what a range yields inside the post-condition of the function, but I can't use code like this, because currently functions with out{} can't use auto as return type:


import std.stdio, std.algorithm, std.traits;
auto foo(int x)
in {
    assert(x >= 0);
} out(result) {
    static assert(is(ForeachType!(typeof(result)) == int));
} body {
    return map!(a => a * 2)([1, 2, 3]);
}
void main() {
    writeln(foo(1));
}


And you have to keep in mind that inside the out{} 'result' is const, and const ranges aren't that useful, you can't even iterate them...

Bye,
bearophile

Reply via email to