[fpc-pascal] Feature announcement: Generic functions, procedures and methods

Anthony Walter sysrpl at gmail.com
Mon Nov 23 10:38:13 CET 2015


Here is another test, a bit more complex. It fails when trying to pass a
procedure reference to the Execute<T, A>. This is a bit different than
trying to get a reference to a generic routine. Routines like ControlMove
and ShapeColor and not generic.

{$mode delphi}

type
  TForEach<T, A> = procedure(Item: T; Arg: A);

// inside TMainForm declaration

procedure Execute<T: TComponent; A>(ForEach: TForEach<T, A>; Arg: A);

// implementation

procedure TMainForm.Execute<T, A>(ForEach: TForEach<T, A>; Arg: A);
var
  I: Integer;
begin
  for I := 0 to ComponentCount - 1 do
    if Components[I] is T then
      ForEach(Components[I] as T, Arg);
end;

procedure ControlMove(Item: TControl; Arg: Integer);
begin
  Item.Left := Arg;
end;

procedure ShapeColor(Item: TShape; Arg: TColor);
begin
  Item.Brush.Color := Arg;
end;

procedure TMainForm.ExecuteButtonClick(Sender: TObject);
begin
  // below fails after "Integer>" with Compilation raised exception
internally
  Execute<TControl, Integer>(ControlMove, Mouse.CursorPos.X);
  if ColorDialog.Execute then
    Execute<TShape, TColor>(ShapeColor, ColorDialog.Color);
end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151123/207a9916/attachment.html>


More information about the fpc-pascal mailing list