forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operatoroverloading.dd
679 lines (547 loc) · 16.4 KB
/
operatoroverloading.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
Ddoc
$(SPEC_S Operator Overloading,
$(P Operator overloading is accomplished by rewriting operators whose
operands are class or struct objects into calls to specially named
member functions. No additional syntax is used.
)
$(UL
$(LI $(RELATIVE_LINK2 Unary, Unary Operator Overloading))
$(LI $(RELATIVE_LINK2 Cast, Cast Operator Overloading))
$(LI $(RELATIVE_LINK2 Binary, Binary Operator Overloading))
$(LI $(RELATIVE_LINK2 equals, Overloading $(D ==) and $(D !=)))
$(LI $(RELATIVE_LINK2 compare, Overloading $(D <), $(D <)$(D =),
$(D <), and $(D <)$(D =)))
$(LI $(RELATIVE_LINK2 FunctionCall, Function Call Operator Overloading))
$(LI $(RELATIVE_LINK2 Assignment, Assignment Operator Overloading))
$(LI $(RELATIVE_LINK2 OpAssign, Op Assignment Operator Overloading))
$(LI $(RELATIVE_LINK2 Array, Index Operator Overloading))
$(LI $(RELATIVE_LINK2 Slice, Slice Operator Overloading))
$(LI $(RELATIVE_LINK2 Dispatch, Forwarding))
)
$(H2 $(LNAME2 Unary, Unary Operator Overloading))
$(TABLE2 Overloadable Unary Operators,
$(THEAD $(I op), $(I rewrite))
$(TROW
$(ARGS $(D -)$(I e)),
$(ARGS $(I e)$(D .opUnary!("-")()))
)
$(TROW
$(ARGS $(D +)$(I e)),
$(ARGS $(I e)$(D .opUnary!("+")()))
)
$(TROW
$(ARGS $(D ~)$(I e)),
$(ARGS $(I e)$(D .opUnary!("~")()))
)
$(TROW
$(ARGS $(D *)$(I e)),
$(ARGS $(I e)$(D .opUnary!("*")()))
)
$(TROW
$(ARGS $(D ++)$(I e)),
$(ARGS $(I e)$(D .opUnary!("++")()))
)
$(TROW
$(ARGS $(D --)$(I e)),
$(ARGS $(I e)$(D .opUnary!("--")()))
)
)
$(P For example, in order to overload the - (negation) operator for struct S, and
no other operator:)
---
struct S {
int m;
int opUnary(string s)() if (s == "-") {
return -m;
}
}
int foo(S s) {
return -s;
}
---
$(H3 Postincrement $(I e)$(D ++) and Postdecrement $(I e)$(D --) Operators)
$(P These are not directly overloadable, but instead are rewritten
in terms of the ++$(I e) and --$(I e) prefix operators:
)
$(TABLE2 Postfix Operator Rewrites,
$(THEAD $(I op), $(I rewrite))
$(TROW
$(ARGS $(I e)$(D --)),
$(ARGS $(D $(LPAREN)auto t =) $(I e)$(D , --)$(I e)$(D , t$(RPAREN))))
$(TROW
$(ARGS $(I e)$(D ++)),
$(ARGS $(D $(LPAREN)auto t =) $(I e)$(D , ++)$(I e)$(D , t$(RPAREN))))
)
$(H3 Overloading Index Unary Operators)
$(TABLE2 Overloadable Index Unary Operators,
$(THEAD $(I op), $(I rewrite))
$(TROW
$(D -)$(I a)$(D [)$(ARGUMENTS)$(D ]),
$(ARGS $(I a)$(D .opIndexUnary!("-")$(LPAREN))$(ARGUMENTS)$(D $(RPAREN))))
$(TROW
$(D +)$(I a)$(D [)$(ARGUMENTS)$(D ]),
$(I a)$(D .opIndexUnary!("+")$(LPAREN))$(ARGUMENTS)$(D $(RPAREN))
)
$(TROW
$(D ~)$(I a)$(D [)$(ARGUMENTS)$(D ]),
$(I a)$(D .opIndexUnary!("~")$(LPAREN))$(ARGUMENTS)$(D $(RPAREN))
)
$(TROW
$(D *)$(I a)$(D [)$(ARGUMENTS)$(D ]),
$(I a)$(D .opIndexUnary!("*")$(LPAREN))$(ARGUMENTS)$(D $(RPAREN))
)
$(TROW
$(D ++)$(I a)$(D [)$(ARGUMENTS)$(D ]),
$(I a)$(D .opIndexUnary!("++")$(LPAREN))$(ARGUMENTS)$(D $(RPAREN))
)
$(TROW
$(D --)$(I a)$(D [)$(ARGUMENTS)$(D ]),
$(I a)$(D .opIndexUnary!("--")$(LPAREN))$(ARGUMENTS)$(D $(RPAREN))
)
)
$(H3 Overloading Slice Unary Operators)
$(TABLE2 Overloadable Slice Unary Operators,
$(THEAD $(I op), $(I rewrite))
$(TROW
$(D -)$(I a)$(D [)$(SLICE)$(D ]),
$(I a)$(D .opSliceUnary!("-")$(LPAREN))$(SLICE2)$(D $(RPAREN))
)
$(TROW
$(D +)$(I a)$(D [)$(SLICE)$(D ]),
$(I a)$(D .opSliceUnary!("+")$(LPAREN))$(SLICE2)$(D $(RPAREN))
)
$(TROW
$(D ~)$(I a)$(D [)$(SLICE)$(D ]),
$(I a)$(D .opSliceUnary!("~")$(LPAREN))$(SLICE2)$(D $(RPAREN))
)
$(TROW
$(D *)$(I a)$(D [)$(SLICE)$(D ]),
$(I a)$(D .opSliceUnary!("*")$(LPAREN))$(SLICE2)$(D $(RPAREN))
)
$(TROW
$(D ++)$(I a)$(D [)$(SLICE)$(D ]),
$(I a)$(D .opSliceUnary!("++")$(LPAREN))$(SLICE2)$(D $(RPAREN))
)
$(TROW
$(D --)$(I a)$(D [)$(SLICE)$(D ]),
$(I a)$(D .opSliceUnary!("--")$(LPAREN))$(SLICE2)$(D $(RPAREN))
)
$(TROW
$(D -)$(I a)$(D [ ]),
$(I a)$(D .opSliceUnary!("-")())
)
$(TROW
$(D +)$(I a)$(D [ ]),
$(I a)$(D .opSliceUnary!("+")())
)
$(TROW
$(D ~)$(I a)$(D [ ]),
$(I a)$(D .opSliceUnary!("~")())
)
$(TROW
$(D *)$(I a)$(D [ ]),
$(I a)$(D .opSliceUnary!("*")())
)
$(TROW
$(D ++)$(I a)$(D [ ]),
$(I a)$(D .opSliceUnary!("++")())
)
$(TROW
$(D --)$(I a)$(D [ ]),
$(I a)$(D .opSliceUnary!("--")())
)
)
$(H2 $(LNAME2 Cast, Cast Operator Overloading))
$(TABLE2 Cast Operators,
$(THEAD $(I op), $(I rewrite))
$(TROW
$(D cast$(LPAREN))$(I type) $(I e)$(D $(RPAREN)),
$(I e)$(D .opCast!$(LPAREN))$(I type)$(D $(RPAREN)())
)
)
$(H3 Boolean Operations)
$(P Notably absent from the list of overloaded unary operators is the !
logical negation operator. More obscurely absent is a unary operator
to convert to a bool result.
Instead, these are covered by a rewrite to:
)
---
opCast!(bool)(e)
---
$(P So,)
---
if (e) => if (e.opCast!(bool))
if (!e) => if (!e.opCast!(bool))
---
$(P etc., whenever a bool result is expected. This only happens, however, for
instances of structs. Class references are converted to bool by checking to
see if the class reference is null or not.
)
$(H2 $(LNAME2 Binary, Binary Operator Overloading))
$(P The following binary operators are overloadable:)
$(TABLE2 Overloadable Binary Operators,
$(TROW $(D +), $(D -), $(D *), $(D /), $(CODE_PERCENT), $(D ^^), $(CODE_AMP))
$(TROW $(CODE_PIPE), $(D ^), $(D <)$(D <), $(D >)$(D >), $(D >)$(D >)$(D >), $(D ~), $(D in))
)
$(P The expression:)
---
a $(METACODE op) b
---
$(P is rewritten as both:)
---
a.opBinary!("$(METACODE op)")(b)
b.opBinaryRight!("$(METACODE op)")(a)
---
$(P and the one with the $(SINGLEQUOTE better) match is selected.
It is an error for both to equally match.
)
$(P Operator overloading for a number of operators can be done at the same time.
For example, if only the + or - operators are supported:)
---
T opBinary(string op)(T rhs) {
static if (op == "+") return data + rhs.data;
else static if (op == "-") return data - rhs.data;
else static assert(0, "Operator "~op~" not implemented");
}
---
$(P To do them all en masse:)
---
T opBinary(string op)(T rhs) {
return mixin("data "~op~" rhs.data");
}
---
$(H2 $(LNAME2 equals, Overloading $(D ==) and $(D !=)))
$(P Expressions of the form $(CODE a != b) are rewritten as $(CODE !(a == b)).)
$(P Given $(CODE a == b) :)
$(OL
$(LI If a and b are both class objects, then the expression is rewritten as:
---
.object.opEquals(a, b)
---
$(P and that function is implemented as:)
---
bool opEquals(Object a, Object b) {
if (a is b) return true;
if (a is null || b is null) return false;
if (typeid(a) == typeid(b)) return a.opEquals(b);
return a.opEquals(b) && b.opEquals(a);
}
---
)
$(LI Otherwise the expressions $(CODE a.opEquals(b)) and
$(CODE b.opEquals(a)) are tried. If both resolve to the same opEquals
function, then the expression is rewritten to be $(CODE a.opEquals(b)).
)
$(LI If one is a better match then the other, or one compiles and the other
does not, the one is selected.)
$(LI Otherwise, an error results.)
)
$(P If overridding Object.opEquals() for classes, the class member function
signature should look like:)
---
class C {
override bool opEquals(Object o) { ... }
}
---
$(P If structs declare an opEquals member function for the identity comparison,
it could have several forms, such as:)
---
struct S {
// lhs should be mutable object
bool opEquals(const S s) { ... } // for r-values (e.g. temporaries)
bool opEquals(ref const S s) { ... } // for l-values (e.g. variables)
// both hand side can be const object
bool opEquals(const S s) const { ... } // for r-values (e.g. temporaries)
}
---
$(P Alternatively, you can declare a single templated opEquals
function with an $(XLINK2 template.html#auto-ref-parameters, auto ref)
parameter:)
---
struct S {
// for l-values and r-values,
// with converting both hand side implicitly to const
bool opEquals()(auto ref const S s) const { ... }
}
---
$(H2 $(LNAME2 compare, Overloading $(D <), $(D <)$(D =), $(D >), and $(D >)$(D =)))
$(P Comparison operations are rewritten as follows:)
$(TABLE2 Overloadable Unary Operators,
$(THEAD comparison, rewrite 1, rewrite 2)
$(TROW $(D a) $(D <) $(D b), $(D a.opCmp(b)) $(D <)
$(D 0), $(ARGS $(D b.opCmp(a)) $(D >) $(D 0)))
$(TROW $(D a) $(D <)$(D= b), $(ARGS $(D a.opCmp(b))
$(D <)$(D= 0)), $(ARGS $(D b.opCmp(a)) $(D >)$(D = 0)))
$(TROW $(D a) $(D >)$(D b), $(ARGS $(D a.opCmp(b))
$(D >) $(D 0)), $(ARGS $(D b.opCmp(a)) $(D <) $(D 0)))
$(TROW $(D a) $(D >)$(D = b), $(ARGS $(D a.opCmp(b))
$(D >)$(D = 0)), $(ARGS $(D b.opCmp(a)) $(D <)$(D= 0)))
)
$(P Both rewrites are tried. If only one compiles, that one is taken.
If they both resolve to the same function, the first
rewrite is done. If they resolve to different functions, the best matching one
is used. If they both match the same, but are different functions, an ambiguity
error results.
)
$(P If overriding Object.opCmp() for classes, the class member function
signature should look like:)
---
class C {
override int opCmp(Object o) { ... }
}
---
$(P If structs declare an opCmp member function, it should follow the following
form:)
---
struct S {
int opCmp(ref const S s) const { ... }
}
---
$(H2 $(LNAME2 FunctionCall, Function Call Operator Overloading $(D f())))
$(P The function call operator, (), can be overloaded by
declaring a function named $(CODE opCall):
)
-------
struct F {
int $(CODE_HIGHLIGHT opCall)();
int $(CODE_HIGHLIGHT opCall)(int x, int y, int z);
}
void test() {
F f;
int i;
i = f(); // same as i = f.opCall();
i = f(3,4,5); // same as i = f.opCall(3,4,5);
}
-------
$(P In this way a struct or class object can behave as if it
were a function.
)
$(P $(CODE static opCall) also works as expected for function call operator with
type names.
)
-------
struct Double {
$(CODE_HIGHLIGHT static) int $(CODE_HIGHLIGHT opCall)(int x) { return x * 2; }
}
void test() {
int i = Double(2);
assert(i == 4);
}
-------
$(P Note that merely declaring $(D opCall) automatically disables
$(XLINK2 struct.html#StructLiteral, struct literal) syntax.
To avoid the limitation, you need to also declare $(XLINK2 Struct-Constructor,
constructor) so that it takes priority over $(D opCall) in $(D Type(...)) syntax.
)
-------
struct Multiplier {
int factor;
this(int num) { factor = num; }
int opCall(int value) { return value * factor; }
}
void test() {
Multiplier m = Multiplier(10); // invoke constructor
int result = m(5); // invoke opCall
assert(result == 50);
}
-------
$(H2 $(LNAME2 Assignment, Assignment Operator Overloading))
$(P The assignment operator $(CODE =) can be overloaded if the
left hand side is a struct aggregate, and $(CODE opAssign)
is a member function of that aggregate.)
For struct types, operator overloading for the identity assignment
is allowed.
---
struct S {
// identiy assignment, allowed.
void $(CODE_HIGHLIGHT opAssign)(S rhs);
// not identity assignment, also allowed.
void $(CODE_HIGHLIGHT opAssign)(int);
}
S s;
s = S(); // Rewritten to s.opAssign(S());
s = 1; // Rewritten to s.opAssign(1);
---
However for class types, identity assignment is not allowed. All class
types have reference semantics, so identity assignment by default rebinds
the left-hand-side to the argument at the right, and this is not overridable.
---
class C {
// If X is the same type as C or the type which is
// implicitly convertible to C, then opAssign would
// accept identity assignment, which is disallowed.
// C opAssign(...);
// C opAssign(X);
// C opAssign(X, ...);
// C opAssign(X ...);
// C opAssign(X, U = defaultValue, etc.);
// not an identity assignment - allowed
void $(CODE_HIGHLIGHT opAssign)(int);
}
C c = new C();
c = new C(); // Rebinding referencee
c = 1; // Rewritten to c.opAssign(1);
---
$(H3 Index Assignment Operator Overloading)
$(P If the left hand side of an assignment is an index operation
on a struct or class instance,
it can be overloaded by providing an opIndexAssign member function.
Expressions of the form $(D a[)$(ARGUMENTS)$(D ] = c) are rewritten
as $(D a.opIndexAssign$(LPAREN)c,) $(ARGUMENTS)$(D $(RPAREN)).
)
-------
struct A {
int $(CODE_HIGHLIGHT opIndexAssign)(int value, size_t i1, size_t i2);
}
void test() {
A a;
a$(CODE_HIGHLIGHT [)i,3$(CODE_HIGHLIGHT ]) = 7; // same as a.opIndexAssign(7,i,3);
}
-------
$(H3 Slice Assignment Operator Overloading)
$(P If the left hand side of an assignment is a slice operation
on a struct or class instance,
it can be overloaded by providing an opSliceAssign member function.
Expressions of the form $(CODE a[)$(SLICE)$(D ] = c) are rewritten
as $(CODE a.opSliceAssign$(LPAREN)c,) $(SLICE2)$(D $(RPAREN)), and
$(CODE a[] = c) as $(CODE a.opSliceAssign(c)).
)
-------
struct A {
int $(CODE_HIGHLIGHT opSliceAssign)(int v); // overloads a[] = v
int $(CODE_HIGHLIGHT opSliceAssign)(int v, size_t x, size_t y); // overloads a[i .. j] = v
}
void test() {
A a;
int v;
a$(CODE_HIGHLIGHT []) = v; // same as a.opSliceAssign(v);
a$(CODE_HIGHLIGHT [)3..4$(CODE_HIGHLIGHT ]) = v; // same as a.opSliceAssign(v,3,4);
}
-------
$(H2 $(LNAME2 OpAssign, Op Assignment Operator Overloading))
$(P The following op assignment operators are overloadable:)
$(TABLE2 Overloadable Op Assignment Operators,
$(TROW $(D +=), $(D -=), $(D *=), $(D /=), $(CODE_PERCENT)$(D =), $(D ^^=), $(CODE_AMP)$(D =))
$(TROW $(CODE_PIPE)$(D =), $(D ^=), $(D <)$(D <)$(D =),
$(D >)$(D >)$(D =), $(D >)$(D >)$(D >)$(D =), $(D ~=), $(NBSP))
)
$(P The expression:)
---
a $(METACODE op)= b
---
$(P is rewritten as:)
---
a.opOpAssign!("$(METACODE op)")(b)
---
$(H3 Index Op Assignment Operator Overloading)
$(P If the left hand side of an $(I op)= is an index expression on
a struct or class instance and opIndexOpAssign is a member:)
---
a[$(METACODE $(ARGUMENTS))] $(METACODE op)= c
---
$(P it is rewritten as:)
---
a.opIndexOpAssign!("$(METACODE op)")(c, $(METACODE $(ARGUMENTS)))
---
$(H3 Slice Op Assignment Operator Overloading)
$(P If the left hand side of an $(I op)= is a slice expression on
a struct or class instance and opSliceOpAssign is a member:)
---
a[$(METACODE $(SLICE))] $(METACODE op)= c
---
$(P it is rewritten as:)
---
a.opSliceOpAssign!("$(METACODE op)")(c, $(METACODE $(SLICE2)))
---
$(P and)
---
a[] $(METACODE op)= c
---
$(P it is rewritten as:)
---
a.opSliceOpAssign!("$(METACODE op)")(c)
---
$(H2 $(LNAME2 Array, Index Operator Overloading))
$(P The array index operator, $(D a[)$(ARGUMENTS)$(D ]), can be overloaded by
declaring a function named $(D opIndex) with one
or more parameters.
)
-------
struct A {
int $(CODE_HIGHLIGHT opIndex)(size_t i1, size_t i2, size_t i3);
}
void test() {
A a;
int i;
i = a[5,6,7]; // same as i = a.opIndex(5,6,7);
}
-------
$(P In this way a struct or class object can behave as if it
were an array.
)
$(P If an index expression can be rewritten using opIndexAssign or opIndexOpAssign,
those are preferred over opIndex.
)
$(H2 $(LNAME2 Slice, Slice Operator Overloading))
$(P Overloading the slicing operator means overloading expressions
like $(CODE a[]) and $(D a[)$(SLICE)$(D ]).
This can be done by declaring a member function named $(CODE opSlice).
)
-------
class A {
int $(CODE_HIGHLIGHT opSlice)(); // overloads a[]
int $(CODE_HIGHLIGHT opSlice)(size_t x, size_t y); // overloads a[i .. j]
}
void test() {
A a = new A();
int i;
int v;
i = a$(CODE_HIGHLIGHT []); // same as i = a.opSlice();
i = a$(CODE_HIGHLIGHT [)3..4$(CODE_HIGHLIGHT ]); // same as i = a.opSlice(3,4);
}
-------
$(P If a slice expression can be rewritten using opSliceAssign or opSliceOpAssign,
those are preferred over opSlice.
)
$(H2 $(LNAME2 Dispatch, Forwarding))
$(P Member names not found in a class or struct can be forwarded
to a template function named $(CODE opDispatch) for resolution.
)
---
import std.stdio;
struct S {
void opDispatch(string s, T)(T i)
{
writefln("S.opDispatch('%s', %s)", s, i);
}
}
class C {
void opDispatch(string s)(int i) {
writefln("C.opDispatch('%s', %s)", s, i);
}
}
struct D {
template opDispatch(string s) {
enum int opDispatch = 8;
}
}
void main() {
S s;
s.opDispatch!("hello")(7);
s.foo(7);
auto c = new C();
c.foo(8);
D d;
writefln("d.foo = %s", d.foo);
assert(d.foo == 8);
}
---
)
Macros:
TITLE=Operator Overloading
WIKI=OperatorOverloading
CATEGORY_SPEC=$0
ARGUMENTS=$(I b)$(SUB 1), $(I b)$(SUB 2), ... $(I b)$(SUB n)
SLICE=$(I i)..$(I j)
SLICE2=$(I i), $(I j)
FOO=