Dynamic records

Language reference ›› Types ›› Records ››
Parent Previous Next

What is called a "dynamic record" in PMP is a special construction of a standard RECORD that accepts static "methods" and "properties".

This is not Object Oriented Programming but a midway between classic programming and OOP. 

It was fully integrated in Delphi some years ago and I found that it may be seen as a new way to manipulate data without all the stuff needed by OOP, so the footprint stays quite small in PIC programming.

 


TYPE
  tMyDynRecord = RECORD
 private
    fX: BYTE;
    function GetX: BYTE;
 public
    B1, B2: BOOLEAN;
    procedure Init(iX: BYTE);
   property X: BYTE read fX write GetX;
  END;

procedure tMyDynRecord.Init(iX: BYTE);
  begin
    fX := iX;
    B1 := true;
    B2 := true;
  end;

function tMyDynRecord.GetX: BYTE;
  begin
    if B1 then Result := 0 else Result := fX;
  end;

VAR
  MyDynR: tMyDynRecord;

 if MyDynR.R = 0 then

 

A method declaration is only allowed after the fields declaration, mixing fields and methods declarations is forbidden.

A method's body must be declared in the implementation section.

 

What dynamic records may do:

Well, first a dynamic record is a record, so it may be used as a record: it may be copied, cleared, used in a WITH statement, passed as a parameter and so on.

Then methods of a dynamic record may manipulate any field of the record without referring to the record itself (implicit WITH SELF clause).

 

What dynamic records do not have:

As this is not real OOP, a dynamic record cannot inherit anything from an "ancestor", so there's no constructors, destructors, virtual methods and any of the OOP concepts.

Compared to Delphi's records, they have no constructors, operators or methods overloading.

Dynamic records are not initialized except by the $INIT RAM directive (as any other variable in PMP).

 

How a dynamic record works:

A dynamic record method has an implicit VAR parameter that is always passed from the caller, called SELF: it is a pointer to the record itself.

To be clear: the method in the example above is implicitly equivalent to:

 


procedure tMyDynRecord.Init(iX, iY: BYTE; var SELF: tMyDynRecord);
  begin
    with SELF do
     begin
       X := iX;
       Y := iY;
       B1 := true;
       B2 := true;
     end;
  end;

 

SELF is a reserved word that refers to the record itself within a dynamic record method only. Mainly, it is used to assign a pointer or to pass the record itself as a parameter.