[fpc-devel] Circular references or Forward Declarations
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
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;
> TChild = forward class; // My made-up syntax
> // or TChild = implementation class; or some other syntax!
> TParent = Class
> property Child :TChild;
> uses ChildObject;
> // Nothing special here, only need the love one-way
> // but you could do it both ways if you want
> unit ChildObject;
> uses ParentObject;
> TChild = Class
> property Parent :TParent;
> "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?
- Graeme -
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
More information about the fpc-devel