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

silvioprog silvioprog at gmail.com
Tue Jun 30 20:54:20 CEST 2015


Hello,

Consider the following sample:

=== begin code ===

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

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private class var
    FMyProc: TMyProc;
  public
    class constructor Create;
    class procedure DoMyProc(ABytes: array of Byte; out AOutput:
AnsiString); overload; static;
    class property MyProc: TMyProc read FMyProc write FMyProc;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  S: AnsiString;
begin
  MyProc([65, 66, 67], S);
  ShowMessage(string(S));
end;

class constructor TForm1.Create;
begin
  FMyProc := @DoMyProc;
end;

class procedure TForm1.DoMyProc(ABytes: array of Byte; out AOutput:
AnsiString);
begin
  SetLength(AOutput, Length(ABytes));
  Move(ABytes[0], AOutput[1], Length(ABytes));
end;

=== end code ===

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?

Thank you!

-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150630/81d1b18c/attachment.html>


More information about the fpc-pascal mailing list