[fpc-devel] Circular references and forward declarations

Juha Manninen juha.manninen at phnet.fi
Tue Jan 5 17:45:27 CET 2010

On tiistai, 5. tammikuuta 2010 18:14:53 Jonas Maebe wrote:
> On 05 Jan 2010, at 17:07, Juha Manninen wrote:
> > Forward declaration of a class is allowed inside a unit. There can
> > be a
> > reference to a class before it is defined! Like:
> >  TMyClass = class;
> >
> > Why is it not allowed from another unit? They are conceptually the
> > same thing!
> The reason that they are conceptually not the same thing is that in
> Pascal two different units can both declare a different class with the
> same name (just like they can both have global variables and
> procedures/functions in the interface with the same name). You would
> at least have to do something like one of the following
> a) use one global name space for all classes (i.e., forbid that two
> different units used in a program declare a class with the same name
> anywhere), or
> b) add some way to specify the unit name in which this external class
> is specified, or
> There might be other solutions (maybe some kind of class-specific
> namespace support), but it would definitely require some more language
> features rather than merely accepting anonymous class definitions
> anywhere. Otherwise the type checking is going to run completely
> haywire.

Yes, I even suggested a syntax:
  TMyClass = class; defined in "MyUnit.pas"

which would solve the namespace issue. It is the case b) you listed.
It could also be:
  MyUnit.TMyClass = class;

which is similar than syntax referencing global variables from another unit.

Juha Manninen

More information about the fpc-devel mailing list