[fpc-pascal] Are there any drawbacks to "reference to"?

Sven Barth pascaldragon at googlemail.com
Mon Jun 20 17:14:00 CEST 2022


Hairy Pixels <genericptr at gmail.com> schrieb am Mo., 20. Juni 2022, 15:30:

>
>
> > On Jun 20, 2022, at 12:44 PM, Sven Barth <pascaldragon at googlemail.com>
> wrote:
> >
> > If the compiler can proove that the function reference never leaves the
> scope (that means no assignment to global variables, out/var parameters,
> the Result variable, passing on to some other function or conversion to
> pointer (and I'm sure there are others)) and the function reference isn't
> used as an interface then and only then the compiler could reduce this to
> something else. But that's an optimization for another day and in my
> opinion such a rarlely used usecase that it's simply not worth it.
>
> Maybe it’s off topic but it’s all very strange to me that FPC is making
> you think in advance what the caller of the function pointer will provide.
> In every other language I’ve used you simply  declare a function pointer
> type and you can give it anything you want, a nested function, global
> function, method or anonymous function and the compiler figures out how to
> call it for you.
>

Then you've never used C++...


> The function reference seems like it could almost fulfill that role but it
> comes with this extra baggage which isn’t always needed and hence the calls
> for optimizing it way.
>
> Having said that, if nothing changes I think the only time you will ever
> use the reference type is if you know it needs to survive beyond the
> calling scope, i.e a thread in most cases.
>

You misjudge how function references are used in common Delphi code. There
is nearly no case where optimizing it away would work. Thus it is simply
not important to optimize this.

Regards,
Sven

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


More information about the fpc-pascal mailing list