www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Default implementation of class .opEquals?

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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:
[...]
 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;
Ooh, very nice! Why didn't I think of that. ;-) Thanks! T -- Don't throw out the baby with the bathwater. Use your hands...
Apr 16 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
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:
 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:
[...]
 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;
Ooh, very nice! Why didn't I think of that. ;-) Thanks!
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... -Steve
Apr 16 2020