Coercing record fields together

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Coercing record fields together

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Coercing record fields together

DaWorm
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

On Mon, Jul 9, 2018, 12:23 PM Ryan Joseph <[hidden email]> 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


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Coercing record fields together

gabor-10
In reply to this post by Ryan Joseph
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:

> 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  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Coercing record fields together

Ryan Joseph


> On Jul 9, 2018, at 11:57 AM, gabor <[hidden email]> wrote:
>
> 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Coercing record fields together

Ralf Quint
In reply to this post by Ryan Joseph
On 7/9/2018 9:22 AM, 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
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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Coercing record fields together

Martin Friebe
In reply to this post by Ryan Joseph
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;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal