On 4/11/12, Andrei Alexandrescu <seewebsiteforem...@erdani.org> wrote: > http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D > > Andrei >
Cool talk! And 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. It might help in cases where the return type is a very complicated template instance and you want to enforce the return expression to be of that type while simultaneously using auto as the return type in the function declaration. It does however do this check *after* any implicit conversions to the return type. So, while my first sample correctly won't compile, the following will compile: import std.traits; import std.algorithm; CommonType!(T1, T2) min(T1, T2)(T1 t1, T2 t2) { return t1; static assert(is(typeof(return) == CommonType!(T1, T2))); } void main() { auto x = min(1, 1.0); } It's interesting to think about.