@yogo1212 first of all:
procedure printAddr(proc: TIntProc);
begin
writeln(hexStr(@proc)); // wrong: you get pointer to pointer
writeln(hexStr(Pointer(proc))); // right: you get pointer to procedure
Great, thank you! I didn't pay too much attention there.
Seems like FPC 2.6.2 only does this "trick" when you assign pointer to variable, not just use it as an argument:
That was my initial suspicion (see third post). Thank you for clarifying, i had hoped for a way to explicitely
reference an overloaded method and i really dont have the background information.
Here is a complete working example, in case anyone might need it:
program project1;
{$mode objfpc}{$H+}
{$T+}
uses
SysUtils;
type
TIntProc = procedure(bla: integer);
TStringProc = procedure(bla: string);
TPIntProc = procedure(bla: PInteger);
procedure ding(bla: integer);
begin
Sleep(bla);
end;
procedure ding(bla: PInteger); overload;
begin
bla^ := 1;
end;
procedure ding(bla: string); overload;
begin
bla := 'meow';
end;
procedure printAddr(proc: TIntProc);
begin
writeln('first:' + #9 + hexStr(proc));
end;
procedure printAddr(proc: TPIntProc); overload;
begin
writeln('second:'+ #9 + hexStr(proc));
end;
procedure printAddr(proc: TStringProc); overload;
begin
writeln('third:'+ #9 + hexStr(proc));
end;
var a: TIntProc;
b: TPIntProc;
c: TStringProc;
begin
a := @ding;
b := @ding;
c := @ding;
// works:
printAddr(a);
printAddr(b);
printAddr(c);
// yields incorrect results:
printAddr(TIntProc(@ding));
printAddr(TPIntProc(@ding));
printAddr(TStringProc(@ding));
end.
I hope this shows both the beauty and the ugliness.
Is this worth a feature request? Being able to name a particular overloaded method?
Like: @dong[String]
While it looks bad, it avoids having to allocate memory for a variable in order to pass the reference to a method.
thank you for sticking around and good night