Nested function requires forward declaration?
Mike Parker
aldacron at gmail.com
Thu Apr 14 10:46:42 UTC 2022
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.
More information about the Digitalmars-d-learn
mailing list