I have shown this to several colleagues and no one has an explanation. I ran into this by pure chance, as I thought I spotted a bug in our code, but was surprised to see that the code actually ran. Here is a simplified version. This has been done with XE-2.
Everyone I have spoke to so far, also expects that a NullReferenceException should be thrown.
TUnexplainable = class(TObject)
public
function Returns19: Integer;
end;
function TUnexplainable.Returns19: Integer;
begin
Result := 19;
end;
The following test should NEVER work, but it runs succesfully. Why is no NullReferenceException thrown????
procedure TTestCNCStep.ShouldNeverEverWorkV4;
var
Impossible: TUnexplainable;
Int1: Integer;
begin
Impossible := nil;
Int1 := Impossible.Returns19; // A Null Reference Exception should ocurr here!!! Instead the method Returns19 is actually invoked!!!!
Check(Int1 = 19);
end;