Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: breaking record parameter down into fields

I have a record type

tLine = record
  X, Y, Count : integer;
  V : boolean;
end;

I have a

function fRotate(zLine: tLine; zAngle: double): tLine;

I want to pass zLine, but with its Y field reduced by 1. Is there a way to break a record down into its specific fields in a procedure or function? I tried

NewLine:=fRotate((zLine.X, zLine.Y-1, zLine.Count, zLine.V), zAngle);

which does not work. Or do I have to do as follows:

dec(zLine.Y);
NewLine:=fRotate(zLine, zAngle);
inc(zLine.Y);

TIA

like image 271
JRBleau Avatar asked Aug 24 '17 13:08

JRBleau


2 Answers

You would typically make a function for this. In modern Delphi with enhanced records, I like to use a static class function like this:

type
  TLine = record
  public
    X: Integer;
    Y: Integer;
    Count: Integer;
    V: Boolean;
  public
    class function New(X, Y, Count: Integer; V: Boolean): TLine; static;
  end;

class function TLine.New(X, Y, Count: Integer; V: Boolean): TLine;
begin
  Result.X := X;
  Result.Y := Y;
  Result.Count := Count;
  Result.V := V;
end;

Then your function call becomes:

NewLine := fRotate(TLine.New(zLine.X, zLine.Y-1, zLine.Count, zLine.V), zAngle);

In older versions of Delphi you'd have to use a function at global scope.

like image 140
David Heffernan Avatar answered Nov 12 '22 20:11

David Heffernan


For readability I like to use an alternative solution with record operators, like this: Note that this is updated in line with Kobik's suggestion

  tLine = record
    X, Y, Count : integer;
    V : boolean;
    class operator Subtract( a : tLine; b : TPoint ) : tLine;
  end;

class operator tLine.Subtract(a: tLine; b : TPoint): tLine;
begin
  Result.X := a.X - b.X;
  Result.Y := a.Y - b.Y;
  Result.Count := a.Count;
  Result.V := a.V;
end;

This allows this type of construct:

  fRotate( fLine - Point(0,1), fAngle );

which I think makes sense. You could obviously use a simple integer rather than an array if all you ever wanted to do was decrement Y, but this allows X and/or Y to be decremented at once.

like image 3
Dsm Avatar answered Nov 12 '22 18:11

Dsm