Hacker News new | ask | show | jobs
by lultimouomo 2245 days ago
> auto whatDoesItReturn(int i)() { static if (i == 0) { return int.init; } else { return string.init; } }

I agree with your point, but for the sake of the audience who doesn't know D I think this example is misleading, as one could take the "int i" parameter as a runtime one, while it's actually a compile time one (the equivalent of C++ non-type template parameter). If you instantiate the function with 0 as a compile-time parameter, it is a function that returns int; otherwise it's a function that returns string. It is never a function that can return int or string.

1 comments

Yeah I realise I could've explained a bit more about template arguments indeed! The point was that it can be hard to specify the return type.