Skip to content

Commit 4121029

Browse files
authored
Merge pull request #3382 from ntrel/cmp-ex
[spec/expression] Tweak CmpExpression docs Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Nicholas Wilson <[email protected]>
2 parents 78883de + 4e57584 commit 4121029

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

spec/expression.dd

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ $(GNAME CmpExpression):
503503
$(GLINK ShiftExpression)
504504
)
505505

506-
$(H2 $(LNAME2 equality_expressions, Equality Expressions))
506+
$(H3 $(LNAME2 equality_expressions, Equality Expressions))
507507

508508
$(GRAMMAR
509509
$(GNAME EqualExpression):
@@ -530,7 +530,7 @@ $(GNAME EqualExpression):
530530
$(P For float, double, and real values, the $(USUAL_ARITHMETIC_CONVERSIONS) are applied
531531
to bring them to a common type before comparison.
532532
The values $(D -0) and $(D +0) are considered equal.
533-
If either or both operands are NAN, then $(D ==) returns false and $(D !=) returns $(D true).
533+
If either or both operands are NaN, then $(D ==) returns false and $(D !=) returns $(D true).
534534
Otherwise, the bit patterns of the common type are compared for equality.
535535
)
536536

@@ -545,7 +545,7 @@ $(GNAME EqualExpression):
545545
x.re == y.re && x.im == y.im
546546
---
547547

548-
$(H3 $(LNAME2 class_struct_equality, Class & Struct Equality))
548+
$(H4 $(LNAME2 class_struct_equality, Class & Struct Equality))
549549

550550
$(P For struct objects, equality means the result of the
551551
$(LINK2 https://dlang.org/spec/operatoroverloading.html#equals, `opEquals()` member function).
@@ -579,7 +579,7 @@ $(GNAME EqualExpression):
579579
operators are intended to compare the contents of the objects,
580580
however an appropriate $(D opEquals) override must be defined for this to work.
581581
The default $(D opEquals) provided by the root $(D Object) class is
582-
equivalent to the $(D is) operator.
582+
equivalent to the $(D is) operator (see below).
583583
Comparing against $(D null) is invalid, as $(D null) has no contents.
584584
Use the $(D is) and $(D !is) operators instead.)
585585

@@ -621,14 +621,28 @@ $(GNAME IdentityExpression):
621621
of elements.
622622
)
623623

624+
$(SPEC_RUNNABLE_EXAMPLE_RUN
625+
---
626+
Object o;
627+
assert(o is null);
628+
629+
auto a = [1, 2];
630+
assert(a is a[0..$]);
631+
assert(a !is a[0..1]);
632+
633+
auto b = [1, 2];
634+
assert(a !is b);
635+
---
636+
)
637+
624638
$(P For other operand types, identity is defined as being the same
625639
as equality.
626640
)
627641

628642
$(P The identity operator $(D is) cannot be overloaded.
629643
)
630644

631-
$(H2 $(LNAME2 relation_expressions, Relational Expressions))
645+
$(H3 $(LNAME2 relation_expressions, Relational Expressions))
632646

633647
$(GRAMMAR
634648
$(GNAME RelExpression):
@@ -642,16 +656,16 @@ $(GNAME RelExpression):
642656
The result type of a relational expression is $(D bool).
643657
)
644658

645-
$(H3 $(LNAME2 array_comparisons, Array comparisons))
659+
$(H3 $(LNAME2 array_comparisons, Array Comparisons))
646660

647-
$(P For static and dynamic arrays, the result of the relational
648-
op is the result of the operator applied to the first non-equal
661+
$(P For static and dynamic arrays, the result of a *CmpExpression*
662+
is the result of the operator applied to the first non-equal
649663
element of the array. If two arrays compare equal, but are of
650664
different lengths, the shorter array compares as "less" than the
651665
longer array.
652666
)
653667

654-
$(H3 $(LNAME2 integer_comparisons, Integer comparisons))
668+
$(H3 $(LNAME2 integer_comparisons, Integer Comparisons))
655669

656670
$(P Integer comparisons happen when both operands are integral
657671
types.
@@ -673,13 +687,13 @@ $(H3 $(LNAME2 integer_comparisons, Integer comparisons))
673687
Use casts to make both operands signed or both operands unsigned.
674688
)
675689

676-
$(H3 $(LEGACY_LNAME2 floating_point_comparisons, floating-point-comparisons, Floating point comparisons))
690+
$(H3 $(LEGACY_LNAME2 floating_point_comparisons, floating-point-comparisons, Floating Point Comparisons))
677691

678692
$(P If one or both operands are floating point, then a floating
679693
point comparison is performed.
680694
)
681695

682-
$(P A relational operator can have `NaN` operands.
696+
$(P A *CmpExpression* can have `NaN` operands.
683697
If either or both operands is `NaN`, the floating point
684698
comparison operation returns as follows:)
685699

@@ -693,11 +707,14 @@ $(H3 $(LEGACY_LNAME2 floating_point_comparisons, floating-point-comparisons, Flo
693707
$(TROW $(D !=),$(ARGS unordered, less, or greater), `true`)
694708
)
695709

696-
$(H3 $(LEGACY_LNAME2 class_comparisons, class-comparisons, Class comparisons))
710+
$(BEST_PRACTICE Although *IdentityExpression* can be used to check for `T.nan`,
711+
there are other floating-point values for NaN produced at runtime.
712+
Use $(REF isNaN, std,math,traits) to handle all of them.)
697713

698-
$(P For class objects, the relational
699-
operators compare the
700-
contents of the objects. Therefore, comparing against
714+
$(H3 $(LEGACY_LNAME2 class_comparisons, class-comparisons, Class Comparisons))
715+
716+
$(P For class objects, *EqualExpression* and *RelExpression* compare the
717+
*contents* of the objects. Therefore, comparing against
701718
a $(CODE null) class reference is invalid, as $(CODE null) has no contents.)
702719

703720
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -711,13 +728,14 @@ $(H3 $(LEGACY_LNAME2 class_comparisons, class-comparisons, Class comparisons))
711728
)
712729

713730
$(P For class objects, the result of `Object.opCmp()` forms the left
714-
operand, and `0` forms the right operand. The result of the
715-
relational expression `(o1 op o2)` is:)
731+
operand, and `0` forms the right operand. The result of an
732+
*EqualExpression* or *RelExpression* `(o1 op o2)` is:)
716733

717734
---
718735
(o1.opCmp(o2) op 0)
719736
---
720737

738+
721739
$(H2 $(LNAME2 in_expressions, In Expressions))
722740

723741
$(GRAMMAR

0 commit comments

Comments
 (0)