views:

250

answers:

3

The following code (constructed only to demonstrate the problem) compiles and works in Delphi 2010. In Delphi 2009, compiler fails with "E2035 Not enough actual parameters".

program Project50;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TMyProc = reference to procedure(param: integer);

var
  a: TProc;
  b: TMyProc;

begin
  b := procedure (param: integer)
    begin
    end;
  a := TProc(b); // <-- [DCC Error] Project50.dpr(19): E2035 Not enough actual parameters
end.

I have found only one very ugly hack to work around the problem (a: TProc absolute b). Does anybody knows of a nicer workaround for this compiler deficiency?

[TProc field is actually hidden inside a record that can store various 'executable' code - TProcedure, TMethod and TProc. Casting is used to store specific anonymous proc into this field.]

+1  A: 

I have found a hack #2:

program Project1;

{$APPTYPE CONSOLE}


uses
  SysUtils;

type
  TMyProc = reference to procedure(param: integer);

var
  a: TProc;
  b: TMyProc;

begin
  b := procedure (param: integer)
    begin
      Writeln('asdf');
    end;
  PPointer(@a)^ := PPointer(@b)^;
  a;
  readln;
end.

I am in doubt what are you trying to achieve by assigning TMyProc (with param argument) to TProc (without argument)?


Updated: A hack #3 (should increment ref counter, the idea is stolen from System._IntfCopy):

procedure AnonCopy(var Dest; const Source);
var
  P: Pointer;

begin
  P:= Pointer(Dest);
  if Pointer(Source) <> nil
    then IInterface(Source)._AddRef;
  Pointer(Dest):= Pointer(Source);
  if P <> nil then
    IInterface(P)._Release;
end;

var
  a: TProc;
  b: TMyProc;

begin
  b := procedure (param: integer)
    begin
      Writeln('asdf');
    end;
  AnonCopy(a, b);
//  PPointer(@a)^ := PPointer(@b)^;
  a;
  readln;
end.
Serg
It works in the test case, but not in my (slightly more complicated) case. Something goes wrong with interface reference counting there. I'll try to put together a more accurate test case.TProc is just a storage area for different 'reference to procedure' entitites, that's why I'm casting it. Maybe something nicer could be done with the generics ...
gabr
@gabr: I have updated my post to propose hack #3 (should increment interface ref counter)
Serg
Thanks for all your work but I've just found a really simple way to solve this issue ...
gabr
At the end I've used your hack #3 approach. Generics are fine if you are limiting yourself to D2009+ but my code must also work in D2007 and conditionally compiling generic/non-generic version of a same type is a mess.
gabr
@gabr: if so you should have a look at System._IntfCopy procedure. I have updated my AnonCopy procedure to decrement "Dest" ref counter, if Dest <> nil
Serg
Thanks for the update!
gabr
A: 

The trick is not to do

a := TProc(b);

but

TMyProc(a) := b;

That compiles and works in D2009. Sample project attached below.

program Project51;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TMyProc = reference to procedure(var param: integer);

  TStorage = record
    FDelegate: TProc;
  end;

var
  a    : TMyProc;
  b    : TMyProc;
  param: integer;
  stg  : TStorage;

begin
  b := procedure (var param: integer)
    begin
      param := 2*param;
    end;
//  stg.FDelegate := TMyProc(b); // doesn't compile in Delphi 2009, compiles in Delphi 2010
  TMyProc(stg.FDelegate) := b;
  param := 21;
  TMyProc(stg.FDelegate)(param);
  Writeln(param);
  Readln;
end.

However, this doesn't work if casting to a local variable.

var
  p: TProc;
  a: TMyProc;

TMyProc(p) := a; // this will not compile

Curiouser and curiouser.

gabr
A: 

It appears that the best way would be to use generics to store the correct type of delegate in the record. No hacks required.

program Project51;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TMyProc = reference to procedure(var param: integer);

  TStorage<T> = record
    FDelegate: T;
  end;

var
  a    : TMyProc;
  b    : TMyProc;
  p    : TProc;
  param: integer;
  stg  : TStorage<TMyProc>;

begin
  b := procedure (var param: integer)
    begin
      param := 2*param;
    end;
  stg.FDelegate := b;
  param := 21;
  stg.FDelegate(param);
  Writeln(param);
  Readln;
end.
gabr