I wrote a quick sample of using method pointers today for someone on IRC and since I only saw one other example in the forums, I figured I'd post it. It's not a tutorial, just an example.
If there is interest I or someone else could write an actual tutorial about it.
Maybe it'll come up in a search if someone needs it.
SCAR Code:
program MethodPointers;
// SCAR V3.20rc
type
// Declare procedure types for the procedures that need to be called
TSimpleMethod = procedure;
// Note that the 'procedure' keyword and parameter list are here,
// but no procedure name.
TFancyMethod = procedure( aString: string );
// Here's a couple of procedures with signatures
// that matche the TSimpleMethod type.
procedure MethodOne;
begin
writeln('method one');
end;
procedure MethodTwo;
begin
writeln('method two' );
end;
// Here's one with a signature that matches the
// TFancyMethod type.
procedure MethodFancy( aString: string );
begin
writeln( 'Fancy ' + aString );
end;
var
// These variables can hold method pointers.
aSimpleMethod : TSimpleMethod;
aFancyMethod : TFancyMethod;
begin
// Set the value of a variable to one of
// the matching procedures
aSimpleMethod := @MethodOne;
// Now call it. Note the () indicating that this
// should call the method, not so something with
// the value of the variable.
aSimpleMethod();
// Reassign the value and call it again.
aSimpleMethod := @MethodTwo;
// Notice that we're using the same name here, but
// the output is different, since it's now actually
// calling MethodTwo instead of MethodOne.
aSimpleMethod();
// This does the same thing, but now it takes a parameter.
aFancyMethod := @MethodFancy;
aFancyMethod( 'smancy' );
end.