Hello I guess I got here late to discussion and bogen85 is away in irc.
Can anyone explain how the anonymous method works ? I’ve never seen procedure something (const a, b, c: integer);
begin
procedure (const d, e, f: integer) begin
Syntax before and didn’t know it was even possible.
Support for anonymous methods in free pascal is a recent addition, from May of this year and available if you clone FPC from source and build and install it.
PascalDragon knows a lot more of the details about this.
They are not in the current release of FPC, but will be available in the next release.
EDIT: Next
major release, as pointed out by @marcov.
An anonymous method is, too make a long answer short, basically a function or procedure declared inline.
To be used it can be:
- Called immediately
- Assigned to a procedure or function reference variable to be called later
- Passed as a parameter in a call that receives a procedure or function reference
An anonymous method has view of all variables and any other identifiers visible in the scope of where it is defined.
In other words, any identifiers that can be used before or after the anonymous method can be used in the anonymous method.
This is true even when the method is passed somewhere else to be called, where that somewhere else does not have the same variable/identifier scope.
This "capturing" of identifiers in it's view that is using so the anonymous method can be called somewhere else is referred to as a closure.
That "view" is part of the anonymous method definition along with the definition you do see.
The anonymous method being directly associated with this closure is what allows it be called elsewhere, where the variable/identifier scope is not necessarily.
From a usage standpoint, anonymous method are similar to procedure and function pointers.
What makes them "special" is the closure that is directly associated with them.
Is this part the anonymous procedure calling itself?
Yes
What keeps this procedure from being called more than once?
It is not assigned to a variable of type reference to procedure.
If it were it could be easily be called more than once.
It is only in the code once, and it only calls itself once.
It is also not in a loop and being called multiple times.