Recent

Author Topic: Help explain this closure example  (Read 1443 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5424
  • Compiler Developer
Re: Help explain this closure example
« Reply #15 on: September 28, 2023, 09:53:07 pm »
1) Does the object get allocated for every function in which a function reference is declared or does an assignment need to occur?

In every function in which a local or anonymous function (which captures local variables), a method, a non-global function pointer or a non-global method pointer is assigned to a function reference. But it's allocated only once per function invocation.

2) I know it's been said from the start that there is some interest in making an option to copy the captured the variables instead of reference them. Does this mean that the variable would be copied upon assignment instead of move/swapping the stack variable to the new object?

It would be copied at the point of assignment of the anonymous or local function to the function reference.

Thanks @Mr.Madguy for the example, that's very useful. If FPC hasn't already they should add something like this to the official manual so programmers actually know what their code REALLY looks like.

No, because that is considered an implementation detail. The only part that is not considered an implementation detail is that a function reference type is in fact a descendant of IUnknown with a single method Invoke.

 

TinyPortal © 2005-2018