[fpc-pascal] Coercing record fields together

Martin fpc at mfriebe.de
Tue Jul 10 13:15:07 CEST 2018


On 09/07/2018 18:22, Ryan Joseph wrote:
> 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;




More information about the fpc-pascal mailing list