Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-03

Assigning class procedures

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michalis Kamburelis

Hi

I'm just migrating a lot of code from delphi to objfpc mode, and I noticed that in objfpc mode I can't do

   MyFunc := @TMyClass.Func;

where TMyClass.Func is a class procedure that matches the type of MyFunc. I'm attaching the complete source code. With fpc -S2 test_1.pas
the attached code doesn't compile, fails with message

Error: Incompatible types: got "<class method type of procedure(LongInt) of object;Register>" expected "<procedure variable type of procedure(LongInt) of object;Register>"

Tested with various FPC versions (2.0.0, 2.0.2, 2.0.3 and 2.1.1 (revision 2911)).

With
fpc -Sd test_1.pas
and
dcc test_1.pas
it compiles OK.

I initially intended to submit this as a bug, but I decided to ask here first. This *is* a bug, right ? I mean, delphi mode behavior seems sensible here, passing class procedures as procedures of objects should be allowed.

Michalis

Reply by email

Peter Vreman
to Michalis Kamburelis
Show quoted text 29 lines
Hi I'm just migrating a lot of code from delphi to objfpc mode, and I noticed that in objfpc mode I can't do MyFunc := @TMyClass.Func; where TMyClass.Func is a class procedure that matches the type of MyFunc. I'm attaching the complete source code. With fpc -S2 test_1.pas the attached code doesn't compile, fails with message Error: Incompatible types: got "<class method type of procedure(LongInt) of object;Register>" expected "<procedure variable type of procedure(LongInt) of object;Register>" Tested with various FPC versions (2.0.0, 2.0.2, 2.0.3 and 2.1.1 (revision 2911)). With fpc -Sd test_1.pas and dcc test_1.pas it compiles OK. I initially intended to submit this as a bug, but I decided to ask here first. This *is* a bug, right ? I mean, delphi mode behavior seems sensible here, passing class procedures as procedures of objects should be allowed.

Class methods require a different parameter for the implicit pushed instance that is not compatible with normal methods. That Delphi allows it is the uglyness of procvar typechecking in Delphi. It results in invalid code and is therefor forbidden in objfpc.

Reply by email

Florian Klaempfl
to Peter Vreman
Peter Vreman wrote 34 lines
> Hi > > I'm just migrating a lot of code from delphi to objfpc mode, and I > noticed that in objfpc mode I can't do > > MyFunc := @TMyClass.Func; > > where TMyClass.Func is a class procedure that matches the type of > MyFunc. I'm attaching the complete source code. With > fpc -S2 test_1.pas > the attached code doesn't compile, fails with message > > Error: Incompatible types: got "<class method type of > procedure(LongInt) of object;Register>" expected "<procedure variable > type of procedure(LongInt) of object;Register>" > > Tested with various FPC versions (2.0.0, 2.0.2, 2.0.3 and 2.1.1 > (revision 2911)). > > With > fpc -Sd test_1.pas > and > dcc test_1.pas > it compiles OK. > > I initially intended to submit this as a bug, but I decided to ask here > first. This *is* a bug, right ? I mean, delphi mode behavior seems > sensible here, passing class procedures as procedures of objects should > be allowed. Class methods require a different parameter for the implicit pushed instance that is not compatible with normal methods. That Delphi allows it is the uglyness of procvar typechecking in Delphi. It results in invalid code and is therefor forbidden in objfpc.
By casting the left side to a pointer it should be eaten by the compiler:
pointer(MyFunc) := @TMyClass.Func;

Reply by email