Event assignment syntax in different Object Pascal dialects
Asked Answered
R

1

11

I'm working on a component that should be shared between Delphi and C++Builder, so I'm using Pascal as the lingua franca. Because I don't have Delphi on my computer at home, I first created the component in the Lazarus IDE. Now I "ported" it to Delphi and found an astonishing syntax problem:

This compiles with FPC (but not Delphi):

FSync.FSyncMethod := @SyncCheckInput;

This compiles with Delphi (but not FPC):

FSync.FSyncMethod := SyncCheckInput;

How can I share a unit between Lazarus and Delphi despite this syntactic divergence?

Redwine answered 22/10, 2015 at 12:25 Comment(6)
"a lack of a common (and portable) synchronizing mechanism" - FreePascal mimics many of Delphi's core classes, include TThread and its Synchronize() method: FreePascal Wiki | Multithreaded Application Tutorial | The TThread ClassConvexoconcave
@RemyLebeau I needed a non-blocking synchronisation mechanism and I got it with (the thread-safe) Application.QueueAsyncCall in Lazarus and PostMessage to an invisible Window (via AllocateHWnd) in Delphi. Of course, I used the TThread class, but Synchronize was not helpful in my case because of its rendezvous feature. Thanks anyway :)Redwine
TThread also has an asynchronous Queue() method.Convexoconcave
@RemyLebeau I'm afraid there is no Queue method in Delphi 4, that I have to use (I'm not sure, I've no D4 or its docs at hand now). So I better remove the sidenote from the question or be more specific about the versions I'm using?Redwine
You did not say which Delphi/FreePascal version you are using. No, Queue() does not exist in D4. FreePascal mimics D7.Convexoconcave
I removed the Note about synchronisation mechanisms. BTW: TThread.Queue does currently not exist in the LCL.Redwine
A
15

Insert this at the beginning of your units:

{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}

This will instruct FreePascal to use the Delphi dialect to compile the unit. Delphi will ignore the {$MODE DELPHI} directive because FPC is not defined.

You can then use this

FSync.FSyncMethod := SyncCheckInput;

for setting events dynamically.

Ailin answered 22/10, 2015 at 12:30 Comment(1)
That's the kind of solution I was hoping for!Redwine

© 2022 - 2024 — McMap. All rights reserved.