find difference between two struct instances.

FoxyBrown via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 21 19:42:04 PDT 2017


On Saturday, 22 July 2017 at 01:04:48 UTC, Nicholas Wilson wrote:
> On Friday, 21 July 2017 at 23:38:51 UTC, FoxyBrown wrote:
>> [...]
>
> use opCmp in conjunction with __traits(allMembers,T)
>
> struct Example
> {
>     int a,b,c;
>     string d,e,f;
> }
>
> void difference(alias func, T)(T t1, T t2) 
> if(__traits(compiles, func(t1,t2)))
> {
>     foreach(U; __traits(allMembers,T)
>     {
>         if (mixin("t1." ~ U.stringof~ ".opCmp(t2." ~ U.stringof 
> ~")")
>             func(t1,t2);
>     }
>
> }
>
> auto a =  Example(1,2,3,"foo","bar",baz");
> auto a =  Example(1,2,42,"foo","bar",quux");
>
> difference!(writeln)(a,b); // hopefully prints 343\nbazquux
>
> Not tested but should give you an idea to adapt as needed.

thanks, I'll try it out.


More information about the Digitalmars-d-learn mailing list