[fpc-pascal] A better way?
Ryan Joseph
ryan at thealchemistguild.com
Thu Apr 14 14:27:23 CEST 2016
> On Apr 14, 2016, at 5:51 PM, Tony Whyman <tony.whyman at mccallumwhyman.com> wrote:
>
> unit unitA;
>
> interface
>
> type
>
> class TClassA
> private
> FClassBObject: TObject;
> public
> procedure SomeProc;
> end;
>
> implementation
>
> uses unitB;
>
> Maybe I’m doing something stupid but other languages have forward declarations so I wonder why Pascal isn’t doing this also since it seems like the obvious solution.
>
>
> procedure TClassA.SomeProc;
> begin
> TClassB(FClassBObject).OtherProc;
> end;
>
> end.
>
> unitB is pretty similar.
>
> As long as you make sure that FClassBObject really is a TClassB object when it is assigned, the above should all work. The only extra effort is with the TClassB(...) wrapper for each reference to FClassBObject.
I use forward declarations sometimes but of course they need to be in the same block so they don’t solve this problem.
Yeah type casting. :) Like interfaces they work but they add a significant overhead and clutter (especially if they need to be used with dozens of methods in a unit) so that’s why I’m seeing if there’s a better way. However I think I like your idea of using them in conjunction with the uses in the implementation instead of my "abstract class" solution. Thanks for the tip.
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list