[fpc-pascal] Are there any drawbacks to "reference to"?
genericptr at gmail.com
Mon Jun 20 15:41:04 CEST 2022
> On Jun 20, 2022, at 8:30 PM, Michael Van Canneyt via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> My point is that if someone creates an API where he/she uses "reference to procedure" then the consumer of the API is stuck with the creation of an interface.
> So it's not always a choice whether an interface will be used or not.
> My conclusion is then that 'reference to' should be used sparingly in public APIs as they are (relatively) expensive.
I just responded with basically this same point. It’s tempting to use a reference because it accepts all function types but it has this baggage. If you’re making an API and want the caller to be able to user nested, global or methods than you need to either duplicate code or use reference to and take the penalty hit.
My opinion is the compiler needs a new type which wraps these all up and lets us just think about function pointers and not what the programmer is going to supply at the time of calling.
If that won’t happen then the references should be strictly limited to instances where you need the call to survive past the calling scope and share state. If you don’t need to share state then I think you could even pass a nested proc var to a thread and it would be called later the same as a reference but the state is captured would be static.
More information about the fpc-pascal