Skip to content

Commit 482d36f

Browse files
authored
Merge pull request #3726 from dkorpel/is-operator-interface
Fix 23972 - class identity check is broken
2 parents aa0a946 + 99530f5 commit 482d36f

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

spec/expression.dd

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,16 +653,44 @@ $(GNAME IdentityExpression):
653653
$(GLINK ShiftExpression) $(D ! is) $(GLINK ShiftExpression)
654654
)
655655

656-
$(P The $(D is) operator compares for identity.
656+
$(P The $(D is) operator compares for identity of expression values.
657657
To compare for nonidentity, use $(D e1 !is e2).
658658
The type of the result is $(D bool). The operands
659659
undergo the $(USUAL_ARITHMETIC_CONVERSIONS) to bring them to a common type before
660660
comparison.
661661
)
662662

663-
$(P For class objects, identity is defined as the object references
664-
are for the same object. Null class objects can be compared with
665-
$(D is).
663+
$(P For class / interface objects, identity is defined as the object references being identical.
664+
Null class objects can be compared with `is`.
665+
Note that inferface objects need not have the same reference of the class they were cast from.
666+
To test whether an `interface` shares a class instance with another `interface` / `class` value, cast both operands to `Object` before comparing with `is`.
667+
)
668+
669+
$(SPEC_RUNNABLE_EXAMPLE_RUN
670+
---
671+
interface I { void g(); }
672+
interface I1 : I { void g1(); }
673+
interface I2 : I { void g2(); }
674+
interface J : I1, I2 { void h(); }
675+
676+
class C : J
677+
{
678+
override void g() { }
679+
override void g1() { }
680+
override void g2() { }
681+
override void h() { }
682+
}
683+
684+
void main() @safe
685+
{
686+
C c = new C;
687+
I i1 = cast(I1) c;
688+
I i2 = cast(I2) c;
689+
assert(i1 !is i2); // not identical
690+
assert(c !is i2); // not identical
691+
assert(cast(Object) i1 is cast(Object) i2); // identical
692+
}
693+
---
666694
)
667695

668696
$(P For struct objects and floating point values, identity is defined as the

0 commit comments

Comments
 (0)