Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-07

Coercing record fields together

6 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

DaWorm
to Ryan Joseph

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

PM Ryan Joseph wrote 30 lines
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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
gabor
to Ryan Joseph: Could I do some RTL magic on a record to loop over its fields and set them to another…

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ł.

W dniu 2018-07-09 o 18:22, Ryan Joseph pisze:

Show quoted text 28 lines
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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Ralf Quint
to Ryan Joseph: Could I do some RTL magic on a record to loop over its fields and set them to another…
Ryan Joseph wrote 19 lines
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

Martin
to Ryan Joseph: Could I do some RTL magic on a record to loop over its fields and set them to another…
Ryan Joseph wrote 20 lines
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;