Discussion:
[fpc-pascal] Coercing record fields together
Ryan Joseph
2018-07-09 16:22:39 UTC
Permalink
Could I do some RTL magic on a record to loop over its fields and set them to another record by name? In the example below I want to set all the fields in TRecA to matching named fields in TRecB. Just curious if we can do this automatically using RTL.

type
TRecA = record
a: string;
b: string;
end;

type
TRecB = record
a: string;
b: string;
c: integer;
d: integer;
end;


for field in recA.GetFields do
recB.GetFieldByName(field.name).value := field.value


Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal
DaWorm
2018-07-09 16:56:23 UTC
Permalink
Just because someone is bound to suggest it, might as well be me.

type
TRecA = record
a: string;
b: string;
end;

type
TRecB = record
A: TRecA;
c: integer;
d: integer;
end;

RecB.A := RecA;

Jeff
Post by Ryan Joseph
Could I do some RTL magic on a record to loop over its fields and set them
to another record by name? In the example below I want to set all the
fields in TRecA to matching named fields in TRecB. Just curious if we can
do this automatically using RTL.
type
TRecA = record
a: string;
b: string;
end;
type
TRecB = record
a: string;
b: string;
c: integer;
d: integer;
end;
for field in recA.GetFields do
recB.GetFieldByName(field.name).value := field.value
Regards,
Ryan Joseph
_______________________________________________
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
gabor
2018-07-09 17:57:41 UTC
Permalink
Hi.
Probably can be done with RTTI (1), enumerators (2) and generics (3)
together.

1. https://stackoverflow.com/questions/27803383/fpc-rtti-on-records
2. http://wiki.freepascal.org/for-in_loop#Traversing_container
3. http://wiki.freepascal.org/Generics

Michał.
Post by Ryan Joseph
Could I do some RTL magic on a record to loop over its fields and set them to another record by name? In the example below I want to set all the fields in TRecA to matching named fields in TRecB. Just curious if we can do this automatically using RTL.
type
TRecA = record
a: string;
b: string;
end;
type
TRecB = record
a: string;
b: string;
c: integer;
d: integer;
end;
for field in recA.GetFields do
recB.GetFieldByName(field.name).value := field.value
Regards,
Ryan Joseph
_______________________________________________
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fp
Ryan Joseph
2018-07-09 18:31:16 UTC
Permalink
Hi.
Probably can be done with RTTI (1), enumerators (2) and generics (3) together.
1. https://stackoverflow.com/questions/27803383/fpc-rtti-on-records
2. http://wiki.freepascal.org/for-in_loop#Traversing_container
3. http://wiki.freepascal.org/Generics
Michał.
Thanks for the tips but ouch that looks complicated, maybe more trouble than it’s worth.

Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fp
Ralf Quint
2018-07-10 04:57:44 UTC
Permalink
Post by Ryan Joseph
Could I do some RTL magic on a record to loop over its fields and set them to another record by name? In the example below I want to set all the fields in TRecA to matching named fields in TRecB. Just curious if we can do this automatically using RTL.
type
TRecA = record
a: string;
b: string;
end;
type
TRecB = record
a: string;
b: string;
c: integer;
d: integer;
end;
for field in recA.GetFields do
recB.GetFieldByName(field.name).value := field.value
If you make sure that any variable of tRecB is declared first, you can use

Var RecB : tRecB;
       RecA : tRedA ABSOLUTE RecB;

Quite obviously, it doesn't work the other way around as tRecB is larger
than tRecA...

Ralf

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin
Martin
2018-07-10 11:15:07 UTC
Permalink
Post by Ryan Joseph
Could I do some RTL magic on a record to loop over its fields and set them to another record by name? In the example below I want to set all the fields in TRecA to matching named fields in TRecB. Just curious if we can do this automatically using RTL.
type
TRecA = record
a: string;
b: string;
end;
type
TRecB = record
a: string;
b: string;
c: integer;
d: integer;
end;
for field in recA.GetFields do
recB.GetFieldByName(field.name).value := field.value
I don't know about the loop, but if records with inheritance would help,
you could do:

type

TRecA = object
a: string;
b: string;
end;

TRecB = object(TRecA)
c: integer;
d: integer;
end;

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fp

Loading...