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;

Comments

  1. It's because the AS operator is resolved in runtime.

    ReplyDelete
  2. An explicit type cast (TA(Foo).Bas:= 123; ) wouldn't be alowed.

    ReplyDelete
  3. XE7 raises a compile-time error, not tried yet with previous versions.

    ReplyDelete
  4. Rafael 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

    ReplyDelete
  5. David Heffernan​ D7 does not complain about this type cast.
    In XE2, the compiler throws the msg "Undeclared identifier: 'Bar'"

    ReplyDelete
  6. +Rafael Ribas Aguiló indeed, which makes this a compiler bug

    ReplyDelete

Post a Comment