Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finalize record passed through the untyped parameter of a function?

Can I pass "any" record type to my procedure ?

Many times I used "records" with strings.

type 
  TR = record
    a: string;
    b: string;
  end;

To clear them, I need to write:

Finalize(R);
FillChar(R, SizeOf(R), #0);

The question is that how to I pass any kind of records to clear it ?

For this I got this hint: "Expression needs no initialize/finalize".

procedure ClearRecord(var R);
begin
  Finalize(R);
  FillChar(R, SizeOf(R), #0);
end;

Thanks for every info!

like image 400
durumdara Avatar asked Dec 20 '22 16:12

durumdara


1 Answers

Do not make it overly complicated. If you don't want to write a two-liner to clear the record, consider declaring:

Const TR_Empty: TR = ();

and use it:

R := TR_Empty;

And as commented by others, a generic procedure to do this is not worth the effort.

If you would have Delphi-2009 or newer, this code is valid for clearing a record:

R := Default(TR);
like image 193
LU RD Avatar answered Dec 24 '22 01:12

LU RD