@@ -70,6 +70,7 @@ $(H2 $(LEGACY_LNAME2 Unary, unary, Unary Operator Overloading))
70
70
$(P For example, in order to overload the $(D -) (negation) operator for struct S, and
71
71
no other operator:)
72
72
73
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
73
74
---
74
75
struct S
75
76
{
@@ -81,11 +82,18 @@ struct S
81
82
}
82
83
}
83
84
84
- int foo(S s )
85
+ void main( )
85
86
{
86
- return -s;
87
+ S s = {2};
88
+ assert(-s == -2);
87
89
}
88
90
---
91
+ )
92
+
93
+ $(NOTE *opUnary* above can also be declared using a template parameter specialization:)
94
+ ---
95
+ int opUnary(string s : "-")()
96
+ ---
89
97
90
98
$(H3 $(LNAME2 postincrement_postdecrement_operators, Postincrement $(I e)$(D ++) and Postdecrement $(I e)$(D --) Operators))
91
99
@@ -97,14 +105,18 @@ $(H3 $(LNAME2 postincrement_postdecrement_operators, Postincrement $(I e)$(D ++)
97
105
$(THEAD $(I op), $(I rewrite))
98
106
$(TROW
99
107
$(ARGS $(I e)$(D --)),
100
- $(ARGS $(D $(LPAREN)auto t =) $(I e)$(D , -- )$(I e)$(D , t$(RPAREN))))
108
+ $(ARGS $(D $(LPAREN)auto t =) $(I e)$(D , )$(I e)`.opUnary!"--"` $(D , t$(RPAREN))))
101
109
$(TROW
102
110
$(ARGS $(I e)$(D ++)),
103
- $(ARGS $(D $(LPAREN)auto t =) $(I e)$(D , ++ )$(I e)$(D , t$(RPAREN))))
111
+ $(ARGS $(D $(LPAREN)auto t =) $(I e)$(D , )$(I e)`.opUnary!"++"` $(D , t$(RPAREN))))
104
112
)
105
113
106
114
$(H3 $(LNAME2 index_unary_operators, Overloading Index Unary Operators))
107
115
116
+ $(P Indexing can be $(RELATIVE_LINK2 array, overloaded).
117
+ A unary operation on an index expression can also be overloaded independently.
118
+ This works for multidimensional indexing.)
119
+
108
120
$(TABLE2 Overloadable Index Unary Operators,
109
121
$(THEAD $(I op), $(I rewrite))
110
122
$(TROW
@@ -132,8 +144,28 @@ $(H3 $(LNAME2 index_unary_operators, Overloading Index Unary Operators))
132
144
)
133
145
)
134
146
147
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
148
+ ---
149
+ struct S
150
+ {
151
+ private int[] a;
152
+
153
+ void opIndexUnary(string s: "++")(size_t i) { ++a[i]; }
154
+ }
155
+
156
+ S s = {[4]};
157
+ ++s[0];
158
+ assert(s.a[0] == 5);
159
+ ---
160
+ )
161
+
135
162
$(H3 $(LNAME2 slice_unary_operators, Overloading Slice Unary Operators))
136
163
164
+ $(P Slicing can be $(RELATIVE_LINK2 slice, overloaded).
165
+ A unary operation on a slice can also be overloaded independently.
166
+ `opIndexUnary` is defined either with no function arguments for a full slice,
167
+ or with two arguments for the start and end indices of the slice.)
168
+
137
169
$(TABLE2 Overloadable Slice Unary Operators,
138
170
$(THEAD $(I op), $(I rewrite))
139
171
$(TROW
@@ -197,7 +229,22 @@ $(H3 $(LNAME2 slice_unary_operators, Overloading Slice Unary Operators))
197
229
)
198
230
)
199
231
200
- $(P For backward compatibility, if the above rewrites fail to compile and
232
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
233
+ ---
234
+ struct S
235
+ {
236
+ private int[] a;
237
+
238
+ void opIndexUnary(string s: "--")() { --a[]; }
239
+ }
240
+
241
+ S s = {[1, 2]};
242
+ --s[];
243
+ assert(s.a == [0, 1]);
244
+ ---
245
+ )
246
+
247
+ $(NOTE For backward compatibility, if the above rewrites fail to compile and
201
248
$(D opSliceUnary) is defined, then the rewrites
202
249
$(D a.opSliceUnary!(op)(i, j)) and
203
250
$(D a.opSliceUnary!(op)) are tried instead, respectively.)
@@ -216,7 +263,7 @@ $(H2 $(LEGACY_LNAME2 Cast, cast, Cast Operator Overloading))
216
263
217
264
$(P Note that `opCast` is only ever used with an explicit `cast`
218
265
expression, except in the case of boolean operations (see next
219
- section))
266
+ section). )
220
267
221
268
$(H3 $(LNAME2 boolean_operators, Boolean Operations))
222
269
0 commit comments