www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opEquals code generation

reply drug <drug2004 bk.ru> writes:
I iterate over struct members and check against equality depending on 
member type. is there more simple/cleaner/better way to achieve this 
functionality? Especially without string mixins?
Sep 19 2017
next sibling parent drug <drug2004 bk.ru> writes:
19.09.2017 15:01, drug пишет:
 I iterate over struct members and check against equality depending on 
 member type. is there more simple/cleaner/better way to achieve this 
 functionality? Especially without string mixins?
oops, https://run.dlang.io/is/PbZE5i
Sep 19 2017
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/19/17 8:01 AM, drug wrote:
 I iterate over struct members and check against equality depending on 
 member type. is there more simple/cleaner/better way to achieve this 
 functionality? Especially without string mixins?
Why not just use tupleof directly instead of having to find the member name and using mixins? -Steve
Sep 19 2017
parent reply drug <drug2004 bk.ru> writes:
19.09.2017 15:38, Steven Schveighoffer пишет:
 On 9/19/17 8:01 AM, drug wrote:
 I iterate over struct members and check against equality depending on 
 member type. is there more simple/cleaner/better way to achieve this 
 functionality? Especially without string mixins?
Why not just use tupleof directly instead of having to find the member name and using mixins? -Steve
Hmm, I'm sure I had tried it before and failed, but now I've managed to do so and it's really simpler (https://run.dlang.io/is/GJkokW): ``` auto opEquals()(auto ref const(typeof(this)) rhs) { import std.math : approxEqual, isNaN; import std.traits : isFloatingPoint, isIntegral; static foreach(i; 0..this.tupleof.length) { { alias FType = typeof(this.tupleof[i]); // a field of this structure auto tf = this.tupleof[i]; // a field of other structure auto of = rhs.tupleof[i]; static if (isFloatingPoint!FType) { if (!tf.isNaN || !of.isNaN) { if (!approxEqual(tf, of)) return false; } } else static if (isIntegral!FType) { if (tf != of) return false; } else static assert (0); } } return true; } ``` Thank you, Steven!
Sep 19 2017
parent reply Neia Neutuladh <neia ikeran.org> writes:
On Tuesday, 19 September 2017 at 13:18:04 UTC, drug wrote:
 19.09.2017 15:38, Steven Schveighoffer пишет:
 On 9/19/17 8:01 AM, drug wrote:
 I iterate over struct members and check against equality 
 depending on member type. is there more simple/cleaner/better 
 way to achieve this functionality? Especially without string 
 mixins?
Why not just use tupleof directly instead of having to find the member name and using mixins? -Steve
Hmm, I'm sure I had tried it before and failed, but now I've managed to do so and it's really simpler
Could be a bit simpler than that, depending on your needs: bool opEquals(Object other) const nothrow nogc { auto f = cast(typeof(this)) other; if (f is null) return false; return this.tupleof == other.tupleof; }
Sep 19 2017
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/19/17 4:28 PM, Neia Neutuladh wrote:

 Could be a bit simpler than that, depending on your needs:
 
 bool opEquals(Object other) const nothrow  nogc
 {
      auto f = cast(typeof(this)) other;
      if (f is null) return false;
      return this.tupleof == other.tupleof;
 }
That doesn't compare floating point in the way he wants. -Steve
Sep 19 2017