[fpc-devel] Assigning to inherited property calls setter of the child class

Mattias Gaertner nc-gaertnma at netcologne.de
Fri May 1 17:39:43 CEST 2009


On Sat, 2 May 2009 02:17:48 +1100
Alexander Klenin <klenin at gmail.com> wrote:

> On Sat, May 2, 2009 at 01:56, Mattias Gaertner
> <nc-gaertnma at netcologne.de> wrote:
> >> 4) If it has setter named "SetP", do the equivalent of "inherited
> >> SetP".
> >
> > Why?
> 
> Because this surprises developers and leads to bugs.
> See the issue http://bugs.freepascal.org/view.php?id=13418
> (linked from the discussed one) for a recent example.

As Jonas already noted:
'That entirely depends on what you expect. "inherited property" now
means "use the property as declared in the parent class", which in a sense is logical.'

You can create a bug report for the documentation that only mentions
methods and maybe this is the reason for the confusion.

For example:

type
  TAncestor = class
  private
    procedure SetP1(const AValue: integer); virtual;
  public
    property P: integer write SetP1;
  end;

  TClassA = class(TAncestor)
  private
    procedure SetP1(const AValue: integer); override;
    procedure SetP2(const AValue: char);
  public
    constructor Create;
    property P: char write SetP2;
  end;

constructor TClassA.Create;
begin
  inherited P:=3; // use property P of the ancestor, 
       // which calls SetP1, which is overridden in TClassA
       // It does *not* call SetP1 of the ancestor
  // To call the ancestor SetP1 use
  inherited SetP1(3);
end;


 
> > "inherited P" should look up P in the ancestor class.
> > The setter of P belongs to the internal implementation of
> > the class. If you want to access the inherited setter, call it with
> > inherited SetP.
> 
> Alternative solution would be to issue a warning if
> TInherited.SetP contains "inherited P := V" and SetP is virtual.

-1

Mattias
 



More information about the fpc-devel mailing list