[fpc-pascal] Are there any drawbacks to "reference to"?
Michael Van Canneyt
michael at freepascal.org
Mon Jun 20 15:39:54 CEST 2022
On Mon, 20 Jun 2022, Hairy Pixels via fpc-pascal wrote:
>> 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.
> 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.
Unless I misunderstood your meaning, not really:
It allows you to run callbacks without having to declare variables globally:
Function DoSomething : String;
L : TStringList;
SomeCallBack(procedure(s : string)
With a regular procedure or procedure of object, you'd need to define L
outside of DoSomething.
More information about the fpc-pascal