@@ -431,13 +431,30 @@ $(OL
431
431
)
432
432
433
433
$(P If overriding $(D Object.opEquals()) for classes, the class member
434
- function signature should look like:)
434
+ function should take an `Object` parameter and dynamically check
435
+ that it is a compatible class, e.g.:)
436
+
437
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
435
438
---
436
439
class C
437
440
{
438
- override bool opEquals(Object o) { ... }
441
+ int i;
442
+
443
+ this(int i) { this.i = i; }
444
+
445
+ override bool opEquals(Object o)
446
+ {
447
+ if (auto c = cast(C) o)
448
+ return c.i == i;
449
+ else
450
+ assert(0, __FUNCTION__ ~ ": Cannot compare a " ~ typeid(o).toString);
451
+ }
439
452
}
453
+
454
+ static assert(new C(2) == new C(2));
455
+ static assert(new C(2) != new C(3));
440
456
---
457
+ )
441
458
442
459
$(P If structs declare an $(D opEquals) member function for the
443
460
identity comparison, it could have several forms, such as:)
@@ -522,7 +539,10 @@ void main()
522
539
}
523
540
---
524
541
$(P If overriding $(D Object.opCmp()) for classes, the class member
525
- function signature should look like:)
542
+ function should take an `Object` parameter and dynamically check
543
+ that it is a compatible class for the comparison (like when overriding
544
+ $(RELATIVE_LINK2 equals, `opEquals`)).)
545
+
526
546
---
527
547
class C
528
548
{
0 commit comments