digitalmars.D - Default implementation of class .opEquals?
- H. S. Teoh (18/18) Apr 16 2020 Just out of curiosity, is there a mixin template or something like that
- Steven Schveighoffer (6/24) Apr 16 2020 This used to work:
- H. S. Teoh (6/17) Apr 16 2020 Ooh, very nice! Why didn't I think of that. ;-) Thanks!
- Steven Schveighoffer (5/20) Apr 16 2020 Hehe, we all didn't think of that. I remember asking the same question I...
Just out of curiosity, is there a mixin template or something like that in Phobos for implementing a default .opEquals that does member-wise comparison of a class object? Something like this: mixin template DefaultOpEquals(T) { bool opEquals(Object o) { auto t = cast(T) o; if (t is null) return false; foreach (field; __traits(getAllMembers, T)) { if (this.field != t.field) return false; } return true; } } It's annoying to have to write an .opEquals method for every class that basically just does the same thing. T -- Verbing weirds language. -- Calvin (& Hobbes)
Apr 16 2020
On 4/16/20 1:21 PM, H. S. Teoh wrote:Just out of curiosity, is there a mixin template or something like that in Phobos for implementing a default .opEquals that does member-wise comparison of a class object? Something like this: mixin template DefaultOpEquals(T) { bool opEquals(Object o) { auto t = cast(T) o; if (t is null) return false; foreach (field; __traits(getAllMembers, T)) { if (this.field != t.field) return false; } return true; } } It's annoying to have to write an .opEquals method for every class that basically just does the same thing.This used to work: this.tupleof == t.tupleof; But you may have to use std.traits or something to get in all the base class fields. -Steve
Apr 16 2020
On Thu, Apr 16, 2020 at 01:32:45PM -0400, Steven Schveighoffer via Digitalmars-d wrote:On 4/16/20 1:21 PM, H. S. Teoh wrote:[...]Just out of curiosity, is there a mixin template or something like that in Phobos for implementing a default .opEquals that does member-wise comparison of a class object? Something like this:Ooh, very nice! Why didn't I think of that. ;-) Thanks! T -- Don't throw out the baby with the bathwater. Use your hands...It's annoying to have to write an .opEquals method for every class that basically just does the same thing.This used to work: this.tupleof == t.tupleof;
Apr 16 2020
On 4/16/20 1:51 PM, H. S. Teoh wrote:On Thu, Apr 16, 2020 at 01:32:45PM -0400, Steven Schveighoffer via Digitalmars-d wrote:Hehe, we all didn't think of that. I remember asking the same question I don't know how many years ago, and someone told me that answer. It's probably somewhere in these forums... -SteveOn 4/16/20 1:21 PM, H. S. Teoh wrote:[...]Just out of curiosity, is there a mixin template or something like that in Phobos for implementing a default .opEquals that does member-wise comparison of a class object? Something like this:Ooh, very nice! Why didn't I think of that. ;-) Thanks!It's annoying to have to write an .opEquals method for every class that basically just does the same thing.This used to work: this.tupleof == t.tupleof;
Apr 16 2020