What are the differences between implementing interfaces in Delphi and Lazarus (FPC)?

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

I think about code interfaces, but I'm not very familiar with them. I would like to know: what are the nuances of implementing the Delphi and Lazarus interfaces? Is there something I should especially know? Do I have to code really different things?

Reference explanation: I think components can benefit from interfaces, or at least I learn more from them. For example, one of the components communicates with many different hardware using a serial port. But the user should use only our component to create the application. So we have a component and one class for each of these hardware, which are descendants of the base class. At run time, we create a specific class inside the component.

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

+6
source share
1 answer

In Free Pascal, the type of interface is mode-specific. Basically, there is COM or CORBA mode. COM is by default and roughly compatible with Delphi. CORBA is a simpler case without reference counting. (and therefore also does not generate calls for recounting functions). So basically the FPC Corba interface is similar to the hypothetical ancestor of the IUnknown interface.

In addition, sometimes there are some differences when releasing interfaces. Delphi tends to save by reducing the recount at the end of the procedure or block (in larger procedures), while the FPC, as you know, releases them early, as a rule, immediately after the approval of the last use. Both variants are logical variants of btw implementation, the basis for which is used in the field of temporary variables. (only at the function level, as well as in deeper nested blocks)

However, this sometimes reveals hidden (bad) assumptions in the code, especially when using interface references and object references within the same procedure, which can survive in Delphi, but not in FPC. This is a typical case, which shows that long-term working code is not necessarily correct. You can only notice hidden assumptions when changing the implementation.

(added later :) note that you can use the COM style on * nix. Basically, it is the insertion of calls into reference counting procedures, which establish two types of interface. Not which system (COM, Corba, or just RTL reference counting) these calls are routed.

Note that I think the COM names for Corba for both interface types were poorly selected. Corba's interfaces are actually recalculated, but traditionally this recalculation is processed manually, since Java does not support automatic interfaces processed externally.

+11
source

Source: https://habr.com/ru/post/910124/


All Articles