What's wrong with this template function?

Neia Neutuladh neia at ikeran.org
Thu Jan 3 21:41:44 UTC 2019


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.


More information about the Digitalmars-d-learn mailing list