[fpc-pascal] A better way?

Ryan Joseph ryan at thealchemistguild.com
Thu Apr 14 11:21:01 CEST 2016


> On Apr 14, 2016, at 2:56 PM, Graeme Geldenhuys <mailinglists at geldenhuys.co.uk> wrote:
> 
> If you can give an actual example we can help. I've used TP then Delphi
> and now Free Pascal for more than 20+ years. I can probably count on one
> hand how many circular reference issues I had. So I dont' think it is
> such a big problem as you make out.
> 
> Often moving uses clause references from the Interface section to the
> Implementation section solve the problem. Sometimes using a base class
> in the interface works. Sometimes using Interfaces (the language
> feature) is a much better approach.
> 
> So again, if you can give an actual example of the various units, and
> how they relate (use each other), then we might be able to help you further.
> 
> Regards,
>  Graeme


I’ve just browsed over some code and found moving uses to the implementation did in fact help. That’s really helpful thank you both. However I’m still seeing some common patterns which just don’t seem Pascal friendly. I started using these more often after using Objective-C on Mac frequently and I really like it but it requires me to hack around the compiler in Pascal.

In that example below the “main” class has children it talks to using an interface and returning a reference to itself for introspection. They are interdependent but Pascal doesn’t offer a way to expose a global namespace for both the units as far as I know.  In other languages I would make another “globals” unit and keep forward references to TClassA, TClassB and IClassA.

==============================

ClassB.pas:

uses
  ClassA;  // <----- circular reference but I need to know about ClassA

type
  TClassB = class (IClassA)
    // when we implement this method we may need to know some things
    // about the parent (TClassA) so it must be included
    procedure ClassDidThis (parent: TClassA; action: integer);
  end;

ClassA.pas:

uses
  ClassB;

type
  TClassA = class
    child: TClassB;
  end;

  // TClassA uses this interface to talk with it's “children” (TClassB)
  // and always returns a reference to itself because the children
  // often need to know about the parent also
  IClassA = interface
    procedure ClassDidThis (parent: TClassA; action: integer);
  end;

Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list