<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Martin Frb via fpc-devel <<a href="mailto:fpc-devel@lists.freepascal.org" target="_blank" rel="noreferrer">fpc-devel@lists.freepascal.org</a>> schrieb am Sa., 5. Apr. 2025, 12:02:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">See the below code. (tested 3.2.3 and 3.3.1)<br>
<br>
Is this intentionally allowed?<br>
<br>
Technically it should work. TSubTest.Test always returns something that <br>
fits the inherited requirement.<br>
<br>
<br>
program Project1;<br>
type<br>
   TFoo = class end;<br>
   TBar = class(TFoo) end;<br>
<br>
   TTest = class<br>
     function Test: TFoo; virtual; abstract;<br>
   end;<br>
<br>
   TSubTest = class(TTest)<br>
     function Test: TBar; override;<br>
   end;<br>
<br>
function TSubTest.Test: TBar;<br>
begin  end;<br>
<br>
begin  end.<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">Yes, this is intentional and is called "covariant return type" ( <a href="https://en.m.wikipedia.org/wiki/Covariant_return_type" target="_blank" rel="noreferrer">https://en.m.wikipedia.org/wiki/Covariant_return_type</a> ). </div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
And if that is intentional, then why not also<br>
<br>
   TTest = class<br>
     procedure Test(a: TBar); virtual; abstract;<br>
   end;<br>
<br>
   TSubTest = class(TTest)<br>
     procedure Test(a: TFoo); override;<br>
   end;<br>
<br>
This fails. Even though any TBar passed will be fine to the base and the <br>
subclass.<br>
<br>
And any<br>
   var sub: TSubTest<br>
will accept TFoo too, but such a var can never hold the base class.<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">This is called contra-variance and is in general not something that programming languages support. See for example here: <a href="https://stackoverflow.com/questions/2995926/why-is-there-no-parameter-contra-variance-for-overriding">https://stackoverflow.com/questions/2995926/why-is-there-no-parameter-contra-variance-for-overriding</a></div><div dir="auto"><br></div><div dir="auto">Regards, </div><div dir="auto">Sven </div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>