[fpc-pascal] Caller agnostic procedure variables

Sven Barth pascaldragon at googlemail.com
Wed Feb 16 11:12:01 CET 2022


Ryan Joseph via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Mi., 16. Feb. 2022, 07:59:

>
>
> > On Feb 16, 2022, at 2:46 AM, Sven Barth via fpc-pascal <
> fpc-pascal at lists.freepascal.org> wrote:
> >
> > // nested function/procedure/routine variable
> > type
> >   TFoobarNested = function: LongInt is nested;
> >
> > var
> >    f: TFoobarFuncRef;
> > begin
> >   // anonymous function/procedure/routine
> >   f := function: LongInt
> >         begin
> >         end;
> > end;
>
> "However assigning a nested function variable to a function reference is
> much harder.
> Assigning a function reference to a nested function variable is hard as
> well. "
>
> This means if you expanded your example with:
>
> var
>   n: TFoobarNested;
> begin
>   f := n;
>
> THAT would be hard? I've never passed around nested function vars before
> so I don't really know the limitations of this. The important thing is the
> primary use case works.
>

Correct. In addition to that the general assumption for function references
is that they can be called even after the function they were assigned to
has been left. For a nested function itself this can hold true as well
(cause the compiler simply needs to transform the nested function correctly
when generating the implementation for the capture object), but for a
nested function variable this assumption would be wrong.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20220216/e5f9b38b/attachment-0001.htm>


More information about the fpc-pascal mailing list