Hi all,
Thanks to Frederic Kehrein FPC main now supports Record Composition:
A record can now "contain" another record and have that record's members
appear as its own.
Firstly this allows easy and automated conversion of C code, which uses
anonymous structs and unions introduced in C11, into Pascal and to use C
libraries frictionless and transparently through the same types and
interfaces.
Secondly it allows for non trailing and multiple variant parts.
And last but not least it can avoid {$IFDef} structures within the
record definition allowing to reuse existing types.
The feature is enabled with
{$modeswitch RecordComposition}
It is not part of any mode by default.
Example:
------------------------
program demo;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
{$modeswitch recordcomposition}
type
TPoint = record
X, Y: Integer;
procedure Offset(dx, dy: Integer);
end;
TSize = record
Width, Height: Integer;
function Area: Integer;
end;
TRectangle = record
contains TopLeft: TPoint; // named composition
contains TSize; // unnamed composition
function Right: Integer;
end;
procedure TPoint.Offset(dx, dy: Integer);
begin
Inc(X, dx);
Inc(Y, dy);
end;
function TSize.Area: Integer;
begin
Result := Width * Height;
end;
function TRectangle.Right: Integer;
begin
Result := X + Width; // composed members, no qualifier needed
end;
var
r: TRectangle;
begin
r.X := 10; // same as r.TopLeft.X
r.Y := 20;
r.Width := 300;
r.Height := 200;
r.Offset(5, 5); // calls TPoint.Offset with Self = r.TopLeft
WriteLn(r.TopLeft.X, ' ', r.Right, ' ', r.Area); // 15 315 60000
end.
------------------------
See here for the more information:
https://wiki.freepascal.org/FPC_New_Features_Trunk#Record_Composition
Mattias
_______________________________________________
fpc-pascal maillist - [email protected]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal