Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
September 19, 2017 opEquals code generation | ||||
---|---|---|---|---|
| ||||
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? |
September 19, 2017 Re: opEquals code generation | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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 |
September 19, 2017 Re: opEquals code generation | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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
|
September 19, 2017 Re: opEquals code generation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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! |
September 19, 2017 Re: opEquals code generation | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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;
}
|
September 19, 2017 Re: opEquals code generation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Neia Neutuladh | 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
|
Copyright © 1999-2021 by the D Language Foundation