[fpc-devel] Circular references or Forward Declarations

Graeme Geldenhuys graemeg at opensoft.homeip.net
Fri Aug 14 08:43:13 CEST 2009


Lance posted this in the Lazarus mailing list, and I think it is more 
appropriate here...


Lance Collins wrote:
 >
 >> How can you avoid circular references with two modules that
 >> interact with each other?
 >
> Basically it should be possible with a minor language change and not too
> much effort from the compiler team. Something like:
> 
> unit ParentObject;
> interface
> TChild = forward class; // My made-up syntax
> // or TChild = implementation class; or some other syntax!
> TParent = Class
> public
>    property Child :TChild;
> end;
> implementation
> uses ChildObject;
> 
> 
> // Nothing special here, only need the love one-way
> // but you could do it both ways if you want
> unit ChildObject;
> interface
> uses ParentObject;
> TChild = Class
> public
>    property Parent :TParent;
> end;
> 
> "TChild = forward class" tells the compiler that TChild is a class, and the
> complete type information is known in the implementation section (which the
> compiler can validate).
 >
 >
 > This feature is often needed and the "if you need to do that then
 > something is wrong with your design" argument doesn't fly with me.


I can't agree more. Sometimes there is a valid design for something like 
that. The compilers already handle forward declarations in the same 
unit, so why can that not be applied for multiple units as well. And if 
the forward declaration is not fully resolved by the time the whole 
project or unit is compiled - raise a compiler error.

The current usage of forward declaration is exactly the same principle 
we are trying to use, but this time just across units. Why can't the FPC 
compiler maybe in ObjFPC mode cater for this?

Regards,
   - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/






More information about the fpc-devel mailing list