Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2014-02

override works, even if result type changed?

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin Frb

Just found that the below works.

Note, that the result type of "a" (overridden) is different. Well, TFoo2 will always satisfy the needs of TFoo, so it seems save. I still found it surprising. Is it intended?

program Project1;
type
   TFoo= class
   public
     function a: TFoo; virtual;
   end;
   TFoo2= class(TFoo)
   public
     function a: TFoo2; override;
   end;
function TFoo2.a: TFoo2;
begin
end;
function TFoo.a: TFoo;
begin
end;
begin
end.

Reply by email

Flávio Etrusco
to Martin Frb
Martin Frb wrote 30 lines
Just found that the below works. Note, that the result type of "a" (overridden) is different. Well, TFoo2 will always satisfy the needs of TFoo, so it seems save. I still found it surprising. Is it intended? program Project1; type TFoo= class public function a: TFoo; virtual; end; TFoo2= class(TFoo) public function a: TFoo2; override; end; function TFoo2.a: TFoo2; begin end; function TFoo.a: TFoo; begin end; begin end.

I hope so :) [1]
But it seems (from a 2011 blog) Delphi still doesn't allow that? I wouldn't mind fpc allowed contravariant arguments too ;) (But I guess I only ever missed it once or twice...)

Regards,
Flávio

[1] http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29

Reply by email

Martin Frb
to Martin Frb: Just found that the below works. Note, that the result type of "a" (overridden) is…

Another question that never got answered. Yet I would expect, that either it is implemented with intend, then someone must know. Or it is a bug then that should be checked?

If no one knows, shall I assume it is a bug, and report it?

Earlier message 39 lines
-------- Original Message -------- Subject: [fpc-devel] override works, even if result type changed? Date: Sun, 23 Feb 2014 16:56:26 +0000 From: Martin Frb Reply-To: FPC developers' list To: FPC developers' list Just found that the below works. Note, that the result type of "a" (overridden) is different. Well, TFoo2 will always satisfy the needs of TFoo, so it seems save. I still found it surprising. Is it intended? program Project1; type TFoo= class public function a: TFoo; virtual; end; TFoo2= class(TFoo) public function a: TFoo2; override; end; function TFoo2.a: TFoo2; begin end; function TFoo.a: TFoo; begin end; begin end.

Reply by email