A thing about typecasting

I had a problem with typecasting the other day and had the workings of typecasting explained to me on Facebook.
Consider this code:

var
  MyFrame : TAbstractFrame;
begin
  MyFrame := GetSomeFrame() as TAbstractFrame;
  MyFrame := TAbstractFame( GetSomeFrame() );
end;

GetSomeFrame returns a frame of TFrame. So I typecast that result into TAbstractFrame (for whatever reason). However, the first typecast (using the ‘as’ structure) fails in my case with an Invalid typecast exception while the next one succeeds.

The solution

Several people responded and explained the difference (or one of the differences anyway) between the two ways to typecase things in Delphi.
The first construct, the one uses as TAbstractFrame will perform a type check and raise the exception Invalid typecast if the types I’m casting from and to does not match. The second construct assumes that you know what you’re doing and accepts the typecast.

Both constructs are syntactically correct, and you’ll have to decide which one you’ll use in the future. I know which one I will use.

In my case, had I realised that the as TAbstractFrame construct failed because what I tried to typecase wasn’t compatible, I would’ve saved an hour or to chasing the elusive bug that caused the Invalid typecast exception in the first place.