Skip to content

Commit 23c740d

Browse files
committed
hash-map.dd: better wording
1 parent a816a21 commit 23c740d

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

spec/hash-map.dd

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ $(HEADERNAV_TOC)
2121
assert(value == 3);
2222
---------
2323

24-
$(P $(BLUE $(B Note:)) The built-in associative arrays do not preserve the order
24+
$(P Neither the $(I KeyType)s nor the element types of an associative
25+
array can be function types or $(D void).
26+
)
27+
28+
$(IMPLEMENTATION_DEFINED The built-in associative arrays do not preserve the order
2529
of the keys inserted into the array. In particular, in a $(D foreach) loop the
26-
order in which the elements are iterated is unspecified.)
30+
order in which the elements are iterated is typically unspecified.)
2731

2832
$(H2 $(LNAME2 removing_keys, Removing Keys))
2933

@@ -59,9 +63,8 @@ $(H2 $(LNAME2 testing_membership, Testing Membership))
5963
}
6064
---------
6165

62-
$(P Neither the $(I KeyType)s nor the element types of an associative
63-
array can be function types or $(D void).
64-
)
66+
$(UNDEFINED_BEHAVIOR Adjusting the pointer to point before or after
67+
the element whose address is returned, and then dereferencing it.)
6568

6669
$(H2 $(LNAME2 using_classes_as_key, Using Classes as the KeyType))
6770

@@ -94,17 +97,27 @@ $(H2 $(LNAME2 using_classes_as_key, Using Classes as the KeyType))
9497
}
9598
---
9699

97-
$(P Care should be taken that $(D toHash) should consistently be the
98-
same value when $(D opEquals) returns true. In other words, two objects
99-
that are considered equal should always have the same hash value. If
100-
this is not the case, the associative array will not function properly.
101-
Also note that $(D opCmp) is not used to check for equality by the
102-
associative array. However, since the actual $(D opEquals) or
100+
$(IMPLEMENTATION_DEFINED
101+
`opCmp` is not used to check for equality by the
102+
associative array. However, since the actual `opEquals` or
103103
`opCmp` called is not decided until runtime, the compiler cannot always
104104
detect mismatched functions. Because of legacy issues, the compiler may
105-
reject an associative array key type that overrides $(D opCmp) but not
106-
$(D opEquals). This restriction may be removed in future versions of
107-
D.)
105+
reject an associative array key type that overrides `opCmp` but not
106+
`opEquals`. This restriction may be removed in future versions.)
107+
108+
$(UNDEFINED_BEHAVIOR
109+
$(OL
110+
$(LI If $(D toHash) must consistently be the
111+
same value when $(D opEquals) returns true. In other words, two objects
112+
that are considered equal should always have the same hash value.
113+
Otherwise, undefined behavior will result.)
114+
))
115+
116+
$(BEST_PRACTICE
117+
$(OL
118+
$(LI Use the attributes `@safe`, `@nogc`, `pure`, `const`, and `scope` as much as possible
119+
on the `toHash` and `opEquals` overrides.)
120+
))
108121

109122
$(H2 $(LNAME2 using_struct_as_key, Using Structs or Unions as the KeyType))
110123

@@ -144,18 +157,29 @@ $(H2 $(LNAME2 using_struct_as_key, Using Structs or Unions as the KeyType))
144157
}
145158
---------
146159

147-
$(P Care should be taken that $(D toHash) should consistently be the
148-
same value when $(D opEquals) returns true. In other words, two structs
149-
that are considered equal should always have the same hash value. If
150-
this is not the case, the associative array will not function properly.)
151-
$(P If necessary the functions can use $(D @trusted) instead of $(D @safe).)
152-
$(P Also note that $(D opCmp) is not used to check for equality by the
160+
$(P The functions can use $(D @trusted) instead of $(D @safe).)
161+
162+
$(IMPLEMENTATION_DEFINED `opCmp` is not used to check for equality by the
153163
associative array. For this reason, and for legacy reasons, an
154164
associative array key is not allowed to define a specialized `opCmp`,
155-
but omit a specialized $(D opEquals). This restriction may be
165+
but omit a specialized `opEquals`. This restriction may be
156166
removed in future versions of D.)
157167

158-
$(H2 $(LNAME2 construction_assignement_entries, Construction or Assignment on Setting AA Entries))
168+
$(UNDEFINED_BEHAVIOR
169+
$(OL
170+
$(LI If $(D toHash) must consistently be the
171+
same value when $(D opEquals) returns true. In other words, two structs
172+
that are considered equal should always have the same hash value.
173+
Otherwise, undefined behavior will result.)
174+
))
175+
176+
$(BEST_PRACTICE
177+
$(OL
178+
$(LI Use the attributes `@nogc` as much as possible
179+
on the `toHash` and `opEquals` overrides.)
180+
))
181+
182+
$(H2 $(LNAME2 construction_assignment_entries, Construction or Assignment on Setting AA Entries))
159183

160184
$(P When an AA indexing access appears on the left side of an assignment
161185
operator, it is specially handled for setting an AA entry associated with

0 commit comments

Comments
 (0)