The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,403 other followers

Reminder to Self: `TProc` is incompatible with parameterless procedures on interfaces

Posted by jpluimers on 2018/04/19

I knew that methods on interfaces were not compatible with the procedure of object (like [WayBack] TProc)or function of object construct, but they are also not compatible with the reference to procedure or reference to function construct.

Via: [WayBack] I try to call an Interface method from TThread.Syncrhonize()…and Berlin don’t accept that… – Paul TOTH – Google+

If you want it fixed, vote for [RSP-13007] Interface methods are not assignable to anonymous method variable – Embarcadero Technologies (Thanks Stefan Glienke).

You can work around it with an anonymous method.

This won’t work:

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils;
 
type
  ITest = interface
    ['{B5AD87E8-A3DF-4B83-BE14-997C2E76A06C}']
    procedure Run;
  end;
 
  TTest = class(TInterfacedObject, ITest)
    procedure Run;
  end;
 
procedure TTest.Run;
begin
  Writeln('PASS')
end;
 
var
  test: ITest; // <- change this to TTest and it compiles
  proc: TProc;
begin
  test := TTest.Create;
  proc := procedure begin test.Run; end; // <- this compiles
  proc := test.Run; // E2010 Incompatible types: 'TProc' and 'procedure, untyped pointer or untyped parameter'
  proc();
  Readln;
end.

–jeroen

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: