Puzzled by this behavior
Don Allen
donaldcallen at gmail.com
Tue May 31 18:30:12 UTC 2022
On Tuesday, 31 May 2022 at 17:52:35 UTC, Adam D Ruppe wrote:
> On Tuesday, 31 May 2022 at 17:41:18 UTC, Don Allen wrote:
>> This strikes me as pretty inconsistent behavior
>
> Code in functions is actually executed in sequence. Nested
> functions aren't exactly code, but the same rule applies to
> them. Consider:
>
> int a = 5;
> a = 6;
> int b = a;
>
> What is b? Of course we know since it happens in sequence. Same
> rule applies with nested functions.
>
>> I've also not found it documented, though I could have missed
>> it (if someone could point me to where this is discussed, I'd
>
> It has its own section on the function page:
>
> https://dlang.org/spec/function.html#nested-declaration-order
Code in Scheme functions are also evaluated in sequence, but
functions can be mutually recursive whether at top-level or not,
so the mere fact of sequential evaluation is not the explanation.
What appears to matter is when the location of called functions
are resolved -- compile-time (and if so, what pass?) or run-time.
In Scheme and Haskell, it is (at least conceptually) at
run-time. D appears to do it both ways, depending on the
particular situation (top-level? nested? static methods?).
Without understanding the reason for this choice, I must say I
find it quite odd.
More information about the Digitalmars-d
mailing list