Skip to content

Commit 2e691c0

Browse files
authored
[spec/expression] Improve EqualExpression docs (#4216)
Use Markdown list for different type behaviour. Clarify that pointers must have a common type. Clarify that array elements must have a common type.
1 parent b4548eb commit 2e691c0

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

spec/expression.dd

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,25 +619,39 @@ $(GNAME EqualExpression):
619619

620620
$(P Inequality is defined as the logical negation of equality.)
621621

622-
$(P If the operands are integral values, the $(USUAL_ARITHMETIC_CONVERSIONS) are applied
622+
- If the operands are integral values, the $(USUAL_ARITHMETIC_CONVERSIONS) are applied
623623
to bring them to a common type before comparison. Equality is defined as the bit patterns
624624
of the common type match exactly.
625-
)
626625

627-
$(P If the operands are pointers, equality is defined as the bit patterns of the operands
628-
match exactly.
629-
)
626+
- If the operands are pointers, equality is defined as the bit patterns of the operands
627+
match exactly. Both types must match, or one can be `typeof(null)`.
630628

631-
$(P For float, double, and real values, the $(USUAL_ARITHMETIC_CONVERSIONS) are applied
629+
- For float, double, and real values, the $(USUAL_ARITHMETIC_CONVERSIONS) are applied
632630
to bring them to a common type before comparison.
633631
The values $(D -0) and $(D +0) are considered equal.
634632
If either or both operands are NaN, then $(D ==) returns false and $(D !=) returns $(D true).
635633
Otherwise, the bit patterns of the common type are compared for equality.
636-
)
637634

638-
$(P For static and dynamic arrays, equality is defined as the
635+
- For static and dynamic arrays, equality is defined as the
639636
lengths of the arrays
640-
matching, and all the elements are equal.
637+
matching, and the elements in each compare equal.
638+
The element types must have a common type.
639+
640+
$(SPEC_RUNNABLE_EXAMPLE_RUN
641+
---
642+
assert(5 == 5L);
643+
assert(byte(4) == 4F);
644+
645+
int i = 1, j = 1;
646+
assert(&i != &j);
647+
assert(&i != null);
648+
649+
// elements of different types are comparable, even when different sizes
650+
int[] ia = ['A', 'B', 'C'];
651+
assert(ia == "ABC");
652+
byte[] ba = [1, 2];
653+
assert(ba == [1F, 2F]);
654+
---
641655
)
642656

643657
$(DDOC_DEPRECATED For complex numbers, equality is defined as equivalent to:)

0 commit comments

Comments
 (0)