digitalmars.D.learn - opEquals code generation
- drug (3/3) Sep 19 2017 I iterate over struct members and check against equality depending on
- drug (2/5) Sep 19 2017 oops, https://run.dlang.io/is/PbZE5i
- Steven Schveighoffer (4/7) Sep 19 2017 Why not just use tupleof directly instead of having to find the member
- drug (37/46) Sep 19 2017 Hmm, I'm sure I had tried it before and failed, but now I've managed to
- Neia Neutuladh (8/21) Sep 19 2017 Could be a bit simpler than that, depending on your needs:
- Steven Schveighoffer (3/11) Sep 19 2017 That doesn't compare floating point in the way he wants.
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
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
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
19.09.2017 15:38, Steven Schveighoffer пишет:On 9/19/17 8:01 AM, drug wrote: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!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
On Tuesday, 19 September 2017 at 13:18:04 UTC, drug wrote:19.09.2017 15:38, Steven Schveighoffer пишет: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; }On 9/19/17 8:01 AM, drug wrote:Hmm, I'm sure I had tried it before and failed, but now I've managed to do so and it's really simplerI 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
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