On 8/20/21 10:37 PM, Alexandru Ermicioi wrote:
On Saturday, 21 August 2021 at 05:34:59 UTC, Alexandru Ermicioi wrote:
...

Also there is no need for mixing string code here. You can get the field using __traits(getMember, this, member).

Cool! Much better. :)

I could not do

  static foreach (member; members) {{
    // ...
  }}

So I am happy with iterating over 0 .. members.length.

// Mixes in opEquals() and opCmp() that perform lexicographical
// comparisons according to the order of 'members'.
mixin template MemberwiseComparison(members...) {
  bool opEquals(const typeof(this) that) const {

    // Comparison code per member, which would potentially return an
    // early 'false' result.
    static foreach (i; 0 .. members.length) {{
      // mixin (makeCode(__traits(identifier, members[i])));
        enum ident = __traits(identifier, members[i]);

if (__traits(getMember, this, ident) != __traits(getMember, that, ident)) {
          return false;
        }
    }}

    // There was no mismatch if we got here.
    return true;
  }

  int opCmp(const typeof(this) that) const {

    // Comparison code per member, which would potentially return an
    // early before or after decision.
    static foreach (i; 0 .. members.length) {{
      enum ident = __traits(identifier, members[i]);

if (__traits(getMember, this, ident) < __traits(getMember, that, ident)) {
        return -1;
      }
if (__traits(getMember, this, ident) > __traits(getMember, that, ident)) {
        return 1;
      }
    }}

    // Neither 'this' nor 'that' was before if we got here.
    return 0;
  }
}

Ali

Reply via email to