What are the differences between implementation of Interfaces in Delphi and Lazarus (FPC)?
Asked Answered
M

1

7

We have a project full of custom components that today is working in Lazarus and Delphi.

I'm thinking in code interfaces on it, but I am not much familiar with them. What I would like to know is: What are the implementation nuances from Delphi and Lazarus interfaces? There is something that I should be specially aware? Will I have to code really different things?

Background explanation: I think the components could benefit from interfaces, or at least, I will learn more from them. For example, one of the components make communication to many different hardwares using serial port. But user should use only our component to create the application. So we have the component and one class to each of this hardware that descendant from a base class. At run-time we create the specific class inside the component.

Not sure this last explanation was needed, but I can write more if any of you need it.

Monocycle answered 6/3, 2012 at 22:4 Comment(7)
Here is a basic reading about the interfaces in FreePascal. As a very quick conclusion I would say, if you are targeting Windows platform and you will use the Windows COM architecture (the interfaces descending from IUnknown) there would be no difference in the code from Delphi, but the answer to your question should be more sophisticated.Conflation
What do you mean by "implementation nuances"? It doesn't sound as though that would actually matter to you. What would matter would be whether or not you can write code that means the same thing to both compilers, irrespective of how each compiler actually implements interfaces.Irresolute
@DavidHeffernan I mean "little differences in the implementation". Maybe I can't write code that means the same to both, but perhaps, I would not need to write all the things for each compiler. I've asked just cause I expected to have, but I don't know if there are any difference in both compilers in this case. Is this clearer now?Monocycle
@Conflation Thanks. I will look that text and maybe compare to embarcadero docs. I'm targeting Windows and the interfaces will only be used inside the application at first. I must admit that I don't know much about this topic. :|Monocycle
Not really much clearer. Sorry.Irresolute
@DavidHeffernan It's ok. English is not my first language. :) Well, first let's understand that when I say implementation, I am not talking about implementation of the interface (Class Implements IcxInterface) but how I can define IcxInterface for the compilers understand. Then, there is anything that is different in Delphi definition of Interfaces and Lazarus definition? Better?Monocycle
Beside was not my first intention when asking this question, differences about using interfaces on FPC and Delphi code are OK too.Monocycle
P
11

In Free Pascal, the interface type depends on mode. Basically there is mode COM or CORBA . COM is default and roughly compatible with Delphi. CORBA is a more simpler case without the reference counting. (and thus also not generating calls to refcounting functions). So basically a FPC Corba interface is like the hypothetical ancestor of the IUnknown interface.

Besides this, there are sometimes some differences wrt when interfaces are released. Delphi tends to save decreasing the refcount at for the end of the procedure or block (in larger procedures), while FPC sometimes is known to release them sooner, typically immediately after the statement of last use. Both are legal implementation choices btw, base on which scope is used for temporary variables. (only on the function level, or also in deeper nested blocks)

However this sometimes reveals hidden (bad) assumptions in code, specially when using interface references and object references within one procedure that might "survive" in Delphi, but not in FPC. It is a typical case that shows that long-time working code is not necessarily correct. One might only notice hidden assumptions when changing implementation

(added later:) note that you can use COM style on *nix. It mainly is the insertion of calls to reference counting routines that set the two interface types apart. Not what system (COM, Corba or simply in RTL reference counting) those calls are routed to.

Note that I think the COM vs Corba names for both interface types were badly chosen. Corba interfaces are refcounted actually, but traditionally this refcount is manually handled, because Java does not support externally handled interfaces in an automated manner.

Added 2021-11-06: It seems that Delphi Alexandria now follows suit

Pilarpilaster answered 8/3, 2012 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.