Compiler directive to avoid circular reference lazarus / 13-General ---------------------------------------------------------------------- Ronan · 2013-02-19 07:19 Dear All, Which compiler directive should I use to avoid the circular reference of units in caller or called unit. I had worm in my heart that my architecture of composing the units and reference to them is completely ill-conditioned in my program but at that stage that I can not go back change the architecture easily. So I simply rely on some hacks and cheats in order to proceed further, Is there any way that whenever compiler come across with possible unit circular reference then it will skip that in the unit definition in the uses clause. your help will be appreciated, Regards, ---------------------------------------------------------------------- BigChimp · 2013-02-19 07:27 AFAIK, the standard solution (apart from reworking the units) is to move some unit references from the interface to the implementation section... No compiler directives involved. ---------------------------------------------------------------------- Ronan · 2013-02-19 07:41 That is not possible in my case because I'll need content of that units right at the interface part, that is, will declare some property which type relies on some other units. ---------------------------------------------------------------------- howardpc · 2013-02-19 09:12 The best (and sometimes the only) way to avoid the problem of circular references ("circles") is to re-design your units in such a way that code required in common by unit A and by unit B is placed in a new third unit C. You almost always find that the benefit of re-thinking the design, and de-coupling sections of code that are not truly inter-dependent gives you a much sounder architecture which is far easier to maintain thereafter, and which is also less likely to throw up hard-to-find bugs. The IDE's Refactoring menu option (right-click in the editor) offers several ways to facilitate such redesign. Adding an implementation uses clause will let you avoid a fundamental redesign for simpler projects that have only a few units, but for larger projects (such as Lazarus itself) more than one developer has written of the problems that can arise from introducing such complex, deeply nested interdependencies, which the compiler seems not always to be able to resolve correctly. ---------------------------------------------------------------------- User137 · 2013-02-19 12:53 Quote from: Ronan on February 19, 2013, 08:41:21 amThat is not possible in my case because I'll need content of that units right at the interface part, that is, will declare some property which type relies on some other units.Should be possible to create 3rd unit, and move the interface part there. Then make first 2 units use that one. ---------------------------------------------------------------------- garlar27 · 2013-02-19 14:18 Once I had the same problem due to a bad design. Without time to do it right I came up with this solution: I created a new unit named customclasses.pas with abstract classes: Code: [Select]TCustomClassB = class; TCustomClassA = Class(TObject)    FObjectB: TCustomClassB;    function MyAction: Boolean; virtual; abstract; end; TCustomClassB = Class(TObject)    FObjectA: TCustomClassA;    function RunAction: Boolean; virtual; abstract; end; then in UnitA.pas: Code: [Select]TClassA = Class(TCustomClassA)    function MyAction: Boolean; override; end; and in UnitB.pas: Code: [Select]TClassB = Class(TCustomClassB)    function RunAction: Boolean; override; end;