[fpc-pascal] Reasoning behind the inability to override a property
leledumbo
leledumbo_cool at yahoo.co.id
Sun Jun 5 23:50:04 CEST 2016
Just my curiosity about this particular "feature", why is property overriding
not allowed (redeclaring with different visibility is however allowed)? e.g.
consider the following code fragment:
type
TA = class
private
FP: Integer;
public
property P: Integer read FP;
end;
TB = class(TA)
private
FP: String;
public
property P: String read FP;
end;
Because TB inherits TA, it can't declare a property named P since TA already
has it.
The real world code having this "problem" is fpWeb:
TFPWebModule = Class(TCustomFPWebModule)
...
end;
TCustomFPWebModule = Class(TSessionHTTPModule)
Public
...
Property Actions : TFPWebActions Read FActions Write SetActions;
...
end;
TFPWebActions = Class(TCustomWebActions)
...
end;
TFPWebAction = Class(TCustomWebAction)
...
end;
TCustomWebActions = Class(TCollection)
public
...
Property Actions[Index : Integer] : TCustomWebAction Read GetActions Write
SetActions; Default;
...
end;
TCustomWebAction = Class(TCollectionItem)
...
end;
The "problem" lies in the Actions property of TCustomWebActions, which
TFPWebActions cannot override, and therefore always returns a
TCustomWebAction, even though the actual type is TFPWebAction, as that's how
it's created:
constructor TCustomFPWebModule.CreateNew(AOwner: TComponent; CreateMode :
Integer);
begin
...
FActions:=TFPWebActions.Create(TFPWebAction);
...
end;
constructor TCustomWebActions.Create(AItemClass: TCollectionItemClass);
begin
inherited Create(AItemClass);
...
end;
This forces the use of typecast when accessing specific action through
TFPWebModule.Actions. The same thing applies to TCustomWebActions.Add which
TFPWebActions doesn't override (it returns TCustomWebAction instead of
TFPWebAction), but can be solved by overriding, just like how
TCustomWebActions.Add overrides TCollection.Add. Hence, only the property
overriding problem left.
--
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Reasoning-behind-the-inability-to-override-a-property-tp5725432.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
More information about the fpc-pascal
mailing list