Another legacy curiosity, this compiled fine in Delphi 7:
Another legacy curiosity, this compiled fine in Delphi 7:
type
TA=class end;
TB=class(TA) Bar: Integer; end;
procedure Test;
var Foo : TB;
begin
(Foo as TA).Bar:=123; // <-- Bar is not in TA !
end;
type
TA=class end;
TB=class(TA) Bar: Integer; end;
procedure Test;
var Foo : TB;
begin
(Foo as TA).Bar:=123; // <-- Bar is not in TA !
end;
It's because the AS operator is resolved in runtime.
ReplyDeleteAn explicit type cast (TA(Foo).Bas:= 123; ) wouldn't be alowed.
ReplyDeleteXE7 raises a compile-time error, not tried yet with previous versions.
ReplyDeleteRafael Ribas Aguiló as is a runtime cast, but the expression Foo as TA has compile time type TA and so this is a compiler bug in D7
ReplyDeleteThis is case three of http://qc.embarcadero.com/wc/qcmain.aspx?d=87818 and has been fixed in XE2 Update 2
ReplyDeleteDavid Heffernan D7 does not complain about this type cast.
ReplyDeleteIn XE2, the compiler throws the msg "Undeclared identifier: 'Bar'"
+Rafael Ribas Aguiló indeed, which makes this a compiler bug
ReplyDelete