How to pass multiple different records (not class due to delphi limitations) to a function?

Posted by mingo on Stack Overflow See other posts from Stack Overflow or by mingo
Published on 2010-03-13T20:50:21Z Indexed on 2010/03/13 20:55 UTC
Read the original article Hit count: 287

Filed under:
|
|
|

Hi to all. I have a number of records I cannot convert to classes due to Delphi limitation (all of them uses class operators to implement comparisons). But I have to pass to store them in a class not knowing which record type I'm using.

Something like this:

type R1 = record
begin 
  x :Mytype;
  class operator Equal(a,b:R1)
end;

type R2 = record
begin 
  y :Mytype;
  class operator Equal(a,b:R2)
end;

type Rn = record
begin 
  z :Mytype;
  class operator Equal(a,b:Rn)
end;

type TC = class
begin
  x : TObject;
  y : Mytype;
  function payload (n:TObject)
end;

function TC.payload(n:TObject)
begin
  x := n;
end;

program:
  c : TC;
  x : R1;
  y : R2;
  ...
  c := TC.Create():
  n:=TOBject(x);
  c.payload(n);

Now, Delphi do not accept typecast from record to TObject, and I cannot make them classes due to Delphi limitation.

Anyone knows a way to pass different records to a function and recognize their type when needed, as we do with class:

if x is TMyClass then TMyClass(x) ... 

???

© Stack Overflow or respective owner

Related posts about delphi

Related posts about records