[Lazarus] Circular references

Michael Van Canneyt michael at freepascal.org
Fri Aug 14 09:29:37 CEST 2009



On Fri, 14 Aug 2009, Graeme Geldenhuys wrote:

> Michael Van Canneyt wrote:
>> 
>> This is the world upside down. Better tell us exactly why you think
>> this must be possible. the 'Sometimes there is a valid design for
>> something like that' is not an argument at all. Maybe there are other
>> - more valid - ways to accomplish what you want.
>
> I can give you loads of examples where I had to implement workarounds in 
> Object Pascal to get around that "limitation".
>
> Have ever tried to write code in a similar fashion as Java does?

No, because I write object pascal, not Java.

> One class 
> per unit. You will hit that issue very quickly. What about having two-way 
> referencing in objects: Customer.GetOrders and Order.GetCustomer.  Or 
> Many-to-Many relationships.

The two way referencing can easily be solved with a forward class
declaration, where the implementation follows in the interface section. 
See TCollection/TCollectionitem.

> For all these you need to revert to Interfaces or use base classes (TObject 
> or TtiObject) and do lots of casting in the implementation section. These are 
> all workarounds for a language (compiler) limitation, which does not exist in 
> other languages like C++ or Java. Alternatively you have to use one HUGE 
> model.pas unit and all your various business classes in there - and maybe 
> even structured via include files and have lots of forward declaration at the 
> top of the model.pas unit. Problem then is that any class can reference any 
> Private or Protected fields in other classes. :-(


If classes are so intertwined, then it seems only natural to put them in 1
unit IMHO. The private/protected issue is valid, though. 
Which is probably why Borland introduced 'Strict private' and 'Strict protected'.

Michael.




More information about the Lazarus mailing list