[fpc-devel] Converting records back and forth. (Forward declarations for records needed ?!?)

Skybuck Flying skybuck2000 at hotmail.com
Mon Sep 19 12:13:52 CEST 2011


Hello,

I want to write code as follows and I will simplify it here to illustrate
the problem.

Let's say I want to kinds of records: TrecordA and TrecordB.

I want to be able to convert types of TrecordA to TrecordB but I also want
to be able to convert types of TrecordB to TrecordA.

So I was thinking of writing code as follows:

type
    TrecordA = record
        function ToRecordB : TrecordB;
    end;

    TrecordB = record
        function ToRecordA : TrecordA;
    end;

These two functions would return a value type/a copy of itself but
converted.

However there is ofcourse a problem, there is a circular reference, and as
far as I know delphi or free pascal cannot forward declare records ?

I could use forward pointers perhaps, but that would be a bit weird and
might lead to bugs because of automatic stack variables/value types being
automatically cleaned up, using pointers would pretty much dismiss the value
types, I could also use dynamic memory but this would create further
problems, since records do no have destructors, the last possible option is
using classes instead of records, so then class forward declarations could
be used, but the records are kinda simply and classes is probably a bit
overkill.

So to me it seems this basic functionality that I seek seems to be missing
from the object pascal/delphi language, so perhaps it can be added to a
future version ?

Bye,
  Skybuck.


 




More information about the fpc-devel mailing list