[fpc-pascal] A better way?

Ryan Joseph ryan at thealchemistguild.com
Thu Apr 14 06:29:42 CEST 2016


The most annoying problem I have with Pascal currently is with circular unit dependanices and “global” classes that need to be referenced from many units. In other languages I would make a forward declaration of the class in  one file then implement it in another file so that all files could reference the class. It’s maybe a symptom of a “bad" design but sometimes it’s just faster and easier so it’s a problem I have to fight Pascal to make it work.

When I moved to FPC the solution I started using was this pattern below where I make an abstract class then override the methods I need in the global namespace within in the “main unit”. This is a bad hack to workaround a feature of the language but I wonder if there’s a better way. Does anyone have any ideas I could try?

========

"global” unit shared by many other units:

type
  TSomeClassAbstract = class (TObject)
    procedure DoSomething; virtual; abstract;
  end;
  
“main” unit which implements the class:  
  
type
  TSomeClass = class (TSomeClassAbstract)
    procedure DoSomething; override;
  end;


Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list