What's wrong with this template function?

Machine Code jckj33 at gmail.com
Fri Jan 4 04:13:13 UTC 2019


On Thursday, 3 January 2019 at 21:41:44 UTC, Neia Neutuladh wrote:
> On Thu, 03 Jan 2019 20:34:17 +0000, Machine Code wrote:
>> Thank you very much, Ali. So the issue was basically I can't 
>> return from a static foreach() loop right?
>
> The static foreach is done at compile time and the return is 
> done at runtime.
>
> After the template is expanded, your code ends up looking like:
>
> Color first()
> {
>     return Color.red;
>     return Color.blue;
>     return Color.green;
>     static assert(false);
> }
>
> And that doesn't compile, because there's a static assert there 
> that fails. It's not at any line of code that would execute at 
> runtime, but the compiler doesn't care about that.

Thank you for the clarification :)


More information about the Digitalmars-d-learn mailing list