[fpc-pascal] cloning data containers

Marc Santhoff M.Santhoff at web.de
Sat Dec 27 16:08:37 CET 2014


I hope you all had nice christmas days and now can answer my
question. ;)

Is there any way of cloning record and objects having only data fields?
Or do I have to copy the (non reference) fields one by one as usual?


a_record = record
  this: integer;
  that: string;

function clone(data_in: a_record): a_record;
  data: Pa_record;

  // here every single data field has to be filled in
  data.this := data_in.this;
  data.that := data_in.that;

  result := data

When you have to deal with lots of record types and lots of fields this
is very boring and maybe exhausting. At least it is error prone without
typing support by some automatism in the IDE.

Using MOVE() is not possible execept if using  {$PackRecord 1}, which is
not always possible and hinders the compiler from generating fast code
(me thinks). And morte, if using record types defined elsewhere (e.g.
TFindRecord) it is impossible.

For classes the same scheme applies, writing an assign() method and
copying each item seperately is a lot of work.

Maybe I have asked this question in the past, but fpc does evolve and
there are e.g. record helpers today. Any help for easing my pain? ;)


Marc Santhoff <M.Santhoff at web.de>

More information about the fpc-pascal mailing list