[fpc-pascal] A better way?
Tony Whyman
tony.whyman at mccallumwhyman.com
Thu Apr 14 12:51:18 CEST 2016
Ryan,
> 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.
Pascal does have forward declarations e.g.
class TClassB;
class TClassA
private
FClassBObject: TClassB;
end;
class TClassB
...
end;
Otherwise, if you really want to avoid defining interdependent classes
in the same unit (not sure why you want to avoid this) then try this:
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.
On 14/04/16 11:35, Ryan Joseph wrote:
>> On Apr 14, 2016, at 5:00 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
>>
>> You should not need TClassB here. You defeat the point of using an
>> interface.
> I’m using the interface for specific communication I want denoted in the interface but I’m still typically using properties of the child class in addition to the interface. Offloading all properties to the interface would work but it would be making accessing them very cumbersome because it requires using Support instead of just accessing them directly.
>
> The interface was probably over complicating the example actually because the true problem is having this pattern of a parent->child relationship where both classes need to know about each other to some extent but putting them in the same unit causes clutter and pollution in other units namespaces. In this example it’s likely that many other units use TClassB and it’s not exclusive to TClassA so putting them in the same unit doesn’t make sense.
>
> 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.
>
> Regards,
> Ryan Joseph
>
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list