Forward declaration is not really a new concept. It was already present in the original Wirth Pascal where it allowed the programmer to do one thing and one thing only – call procedure A from procedure B and procedure B from procedure A. Remember – in ye olde times of good old Pascal we had no interfaces, no classes, no units … just procedures and functions.
Because the code tells more than thousand words, here’s an example (still perfectly valid in modern Object Pascal).
procedure ProcA; forward; procedure ProcB; begin ProcA; end; procedure ProcA; begin ProcB; end;
A more familiar concept in modern times is forward declaration for classes and interfaces. Again, a code tells more than a long explanation …
type TClassA = class; TClassB = class ObjA: TClassA; end; TClassA = class ObjB: TClassB; end; IIntfA = interface; IIntfB = interface function Other: IIntfA; end; IIntfA = interface function Other: IIntfB; end;
Object Pascal, however, does not understand the concept of a forward record declaration. IOW, this will not compile.
type TRecA = record; TRecB = record function Other: TRecA; end; TRecA = record function Other: TRecB; end;
There’s however a trick that allows us to achieve the same functionality with a different syntactic sugar – record helpers. We can remove the TRecB.Other declaration from TRecB and reintroduce it later via a TRecB helper.
type TRecB = record end; TRecA = record function Other: TRecB; end; TRecBHelper = record helper for TRecB function Other: TRecA; end;
Just keep in mind that this solution is a bit less stable as other code can hide away the functionality from TRecBHelper by introducing their own helper for TRecB. (A nasty “feature” of the language which really should be fixed loooong ago.)