On Thursday, 14 April 2022 at 08:55:25 UTC, Chris Katko wrote:

I imagine this is a really odd edge case but it's piqued my interest.

Consider this:

```d
void main() {
    void foo() { initRuntimeState(i); }
    foo();
    if(!modifyRutimeState()) return;
    int i = getRandomValue();
    i = returnSomethingBasedOnRuntimeState(i);
}
```

Which value of `i` should `foo` use? What if `modifyRuntimeState` and `returnSomethingBasedOnRuntimeState` are dependent on `initRuntimeState`?

At module scope, and in class/struct declarations, only compile-time values can be used to initialize variables and assignment is illegal, so the compiler can jump around initializing things in any order it wants. It has a well-defined and limited set of rules it can work with.

That just isn't the case in local scopes, where a variable's lifetime begins at the point of declaration, the scope can exit at any time, and the order of evaluation can have side effects that change the run-time state.

Reply via email to