[fpc-devel] Overridden method with different signature - compiles - should it?

Martin Frb lazarus at mfriebe.de
Sat Apr 5 12:02:43 CEST 2025


See the below code. (tested 3.2.3 and 3.3.1)

Is this intentionally allowed?

Technically it should work. TSubTest.Test always returns something that 
fits the inherited requirement.


program Project1;
type
   TFoo = class end;
   TBar = class(TFoo) end;

   TTest = class
     function Test: TFoo; virtual; abstract;
   end;

   TSubTest = class(TTest)
     function Test: TBar; override;
   end;

function TSubTest.Test: TBar;
begin  end;

begin  end.


And if that is intentional, then why not also

   TTest = class
     procedure Test(a: TBar); virtual; abstract;
   end;

   TSubTest = class(TTest)
     procedure Test(a: TFoo); override;
   end;

This fails. Even though any TBar passed will be fine to the base and the 
subclass.

And any
   var sub: TSubTest
will accept TFoo too, but such a var can never hold the base class.


More information about the fpc-devel mailing list