delphirttitypeinfo

Typeinfo with record type does not work at runtime


I have a little problem with compilation under Delphi:

function T_QS2ProcessMailbox.PutRec<T>(const aID: T_Barcode; var aRec: T;const aTxt: String): Boolean;
var
  FA: T_FahrauftragRec absolute aRec;
  LP: T_LagerpackungRec absolute aRec;
begin
  init_Rec;
  Rec.ID        := aID;
  Rec.EventTime := Now;
  Rec.Text      := aTxt;
  if TypeInfo(T_LagerpackungRec) = TypeInfo(T) then
  begin
    Rec.RecType := C_QS_TYPE_TLAGERPACKUNGREC;
    Rec.FA      := FA;
  end
  else
    if Typeinfo(T) = Typeinfo(T_LagerpackungRec) then
    begin
      Rec.RecType := C_QS_TYPE_TFAHRAUFTRAGREC;
      Rec.LP      := LP;
    end
    else
      Rec.RecType := C_QS_TYPE_TEXT;
  Send_TraceMsg(ClassName + '.PutRec Type=' + IntToStr(Rec.RecType));
  Result        := PutRec(Rec);
end;

It compiles fine without errors, messages, or hints. But it is compiled without if statements. You can look at it in the picture - this code without compilations marker

image

I do not understand why.

Can somebody explain to me what I am doing incorrectly?


Solution

  • Those if statements can be resolved at compile time, so only ever 1 of them will be actually compiled for any given value of T. (In other word, the compiled code will never execute any if for this function).

    I can imagine 2 reasons for seeing only 1 compilation marker. Either your application will only ever use 1 of the if statements, or the IDE will map the compilation marker of all the if statements to the same line (I find this last one unlikely, but I've seen stranger things in the IDE).

    Another possibility is that your 2nd if should read

    if Typeinfo(T) = Typeinfo(T_FahrauftragRec) then
    

    instead of

    if Typeinfo(T) = Typeinfo(T_LagerpackungRec) then