[fpc-pascal] How to use a static method as procedure variable?

Ewald ewald at yellowcouch.org
Sun Jul 5 19:41:47 CEST 2015

On 30 Jun 2015, at 20:54, silvioprog wrote:

> The code above compiles fine in Delphi (tested in XE 7), but in FPC I got the following compiling error:
> Unit1.pas(46,14) Error: Incompatible types: got "<class method type of procedure({Open} Array Of Byte;out AnsiString) of object;Register>" expected "<procedure variable type of procedure({Open} Array Of Byte;out AnsiString);Register>".
> Is this a bug or I need to enable some switch?

Back in 2012 I had a similar issue. Original message can be found here: http://lists.freepascal.org/pipermail/fpc-pascal/2012-December/035921.html

I fixed it by declaring a type that includes the hidden parameter mentioned in that thread, so in your case:

		TMyProc = procedure(aClassType: TClass; ABytes: array of Byte; out AOutput: AnsiString);

the invocation then becomes
	FMyProc(ClassType, [65, 66, 67], S);

instead of 
	FMyProc([65, 66, 67], S);

And, of course, the assignment to FMyProc becomes more verbose and less type-safe: 
	FMyProc := TMyProc(Pointer(@DoMyProc));

NOTE: This is a hack. It is not documented anywhere, but i have used this approach since late 2012 without any problems so far. Better be careful and write a small sanity test that checks correct operation every time you use a new compiler.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150705/68c18cc0/attachment.html>

More information about the fpc-pascal mailing list