[fpc-pascal] Why does nested function get corrupted?

Sven Barth pascaldragon at googlemail.com
Sun Sep 11 14:22:11 CEST 2022


Ondrej Pokorny via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
So., 11. Sep. 2022, 12:18:

> Am 11.09.2022 um 11:26 schrieb Sven Barth via fpc-pascal:
>
> Hairy Pixels <genericptr at gmail.com> schrieb am Sa., 10. Sep. 2022, 03:21:
>
>>
>>
>> > On Sep 9, 2022, at 4:48 PM, Sven Barth <pascaldragon at googlemail.com>
>> wrote:
>> >
>> > How about you simply report such corruptions as bugs? I can always
>> close them as "not a bug" or duplicate if necessary.
>> >
>>
>> Well I thought the data may have been saved on the stack and thus lost
>> when the function exits. What is the expected behavior for passing these
>> outside of the calling scope?
>>
>
> Looking at your example again - I'm only on my phone currently - it's
> indeed your fault because the state passed to a nested function variable
> does *not* survive the stack frame it belongs to. That's where function
> references shine, because there the state *does* survive.
>
> Shouldn't this assignment be forbidden?
>
> type
>   TProc = procedure is nested;
> function TestNested: TProc;
> begin
>   result := procedure // why is this possible?
>   begin
>     writeln(data);
>   end;
>
> It is not a nested function but an anonymous one.
>
>
And? An anonymous function is assignment compatible to multiple left sides
depending on what it captures. See my announcement mail.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20220911/4c04bae8/attachment.htm>


More information about the fpc-pascal mailing list