[fpc-pascal] Selecting Records with a variable
Stefan V. Pantazi
svpantazi at gmail.com
Sun Dec 20 22:01:34 CET 2020
James, I remember going through some similar motions as you are. Looks like
you are searching for a form of indexing your record fields.
So many good suggestions, I had to join the fray and try something... My
preferred approach would also be an ordinal type for the axes and a
record variant, as already suggested. Objects and advanced records would be
fine too. That way you get to keep both worlds while evolving your software
to a more elegant approach along the lines of some of the suggested
solutions.
Here is something I tried on top of your example to make sure it actually
works.
program example1;
Type
Axis_Name=(anX,anY,anZ,anA,anB,anC);
Axis_Array=array[Axis_Name] of Double;
Axis_Record = Record
X,Y,Z,A,B,C: Double;
End;
Axis_Record_variant = Record
case boolean of
false: (AxRec:Axis_Record);
true: (AxArr:Axis_Array);
End;
Var
AxisValueVar : Axis_Record_variant;
AxisValue : Axis_Record absolute AxisValueVar.AxRec;
//Instead of this:
Procedure ShowAxis(Axisletter:Char);
Begin
If AxisLetter = 'X' Then
Writeln(AxisValue.X);
If AxisLetter = 'Y' Then
Writeln(AxisValue.Y);
If AxisLetter = 'Z' Then
Writeln(AxisValue.Z);
If AxisLetter = 'A' Then
Writeln(AxisValue.A);
If AxisLetter = 'B' Then
Writeln(AxisValue.B);
If AxisLetter = 'C' Then
Writeln(AxisValue.C);
End;
//I would rather have something like this:
Procedure ShowAxisVariant(AxisName:Axis_Name);
Begin
Writeln(AxisValueVar.AxArr[AxisName]);
End;
begin
AxisValue.Z := 1234.5678;
ShowAxis('Z');
ShowAxisVariant(anZ);
end.
On Sun, Dec 20, 2020 at 2:01 PM James Richters via fpc-pascal <
fpc-pascal at lists.freepascal.org> wrote:
> Yes, I was trying to keep my data structure, because there were thousands
> of occurrences ..
> This project has had that structure since Turbo Pascal back in the 80s
> when it only had 3 axis
> It seemed like a good way to have variables all with the same name, and
> back then it wasn't so bad to have repeated code,
> but now I have 9 axis and it's getting ridiculous.. once I realized that
> Arrays would allow me to simplify things greatly,
> it turned out to not be so bad to change it... just a bunch of global
> search and replaces and a little fixing here and there that actually made
> it cleaner,
> and the whole project has been changed over in a relatively short amount
> of time... and now I can start consolidating all those functions and
> procedures.
> It was way less work to just change it all to a better structure than it
> was to work around it.
>
> James
>
> >I think this is a typical example where properly thinking about a
> solution for a programming issue BEFORE going with
> >what seems as the logical choice at first glance leads to a much simpler
> solution without coming up with all kinds of fancy workarounds...
>
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20201220/93813a86/attachment-0001.htm>
More information about the fpc-pascal
mailing list