Skip to content

Commit 3387f76

Browse files
authored
[spec/operatoroverloading] Improve docs (#4232)
Fix formatting. Make some examples runnable. Fix comment about `opSlice` lowering. Fix `opDollar` example.
1 parent 092706f commit 3387f76

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

spec/operatoroverloading.dd

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ T opBinary(string op)(T rhs)
345345
{
346346
static if (op == "+") return data + rhs.data;
347347
else static if (op == "-") return data - rhs.data;
348-
else static assert(0, "Operator "~op~" not implemented");
348+
else static assert(0, "Operator " ~ op ~ " not implemented");
349349
}
350350
---
351351

@@ -354,7 +354,7 @@ T opBinary(string op)(T rhs)
354354
---
355355
T opBinary(string op)(T rhs)
356356
{
357-
return mixin("data "~op~" rhs.data");
357+
return mixin("data " ~ op ~ " rhs.data");
358358
}
359359
---
360360

@@ -607,14 +607,15 @@ $(H2 $(LEGACY_LNAME2 FunctionCall, function-call, Function Call Operator Overloa
607607
declaring a function named $(CODE opCall):
608608
)
609609

610+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
610611
-------
611612
struct F
612613
{
613-
int $(CODE_HIGHLIGHT opCall)();
614-
int $(CODE_HIGHLIGHT opCall)(int x, int y, int z);
614+
int opCall();
615+
int opCall(int x, int y, int z);
615616
}
616617

617-
void test()
618+
void main()
618619
{
619620
F f;
620621
int i;
@@ -623,6 +624,7 @@ $(H2 $(LEGACY_LNAME2 FunctionCall, function-call, Function Call Operator Overloa
623624
i = f(3,4,5); // same as i = f.opCall(3,4,5);
624625
}
625626
-------
627+
)
626628

627629
$(P In this way a struct or class object can behave as if it
628630
were a function.
@@ -660,17 +662,20 @@ $(H3 $(LNAME2 static-opcall, Static opCall))
660662
type names.
661663
)
662664

665+
$(SPEC_RUNNABLE_EXAMPLE_RUN
663666
-------
664667
struct Double
665668
{
666-
$(CODE_HIGHLIGHT static) int $(CODE_HIGHLIGHT opCall)(int x) { return x * 2; }
669+
static int opCall(int x) { return x * 2; }
667670
}
668-
void test()
671+
672+
void main()
669673
{
670674
int i = Double(2);
671675
assert(i == 4);
672676
}
673677
-------
678+
)
674679

675680
$(P Mixing struct constructors and $(D static opCall) is not allowed.)
676681

@@ -971,7 +976,7 @@ struct S
971976
void main()
972977
{
973978
auto s = S([1, 2, 3]);
974-
int[] t = s[0..2]; // calls s.opIndex(s.opSlice(0, 2))
979+
int[] t = s[0..2]; // calls s.opIndex(s.opSlice!0(0, 2))
975980
assert(t == [1, 2]);
976981
}
977982
---
@@ -1075,16 +1080,18 @@ $(H3 $(LEGACY_LNAME2 Dollar, dollar, Dollar Operator Overloading))
10751080
understood by $(D opSlice) and $(D opIndex).
10761081
)
10771082

1083+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
10781084
------
10791085
struct Rectangle
10801086
{
10811087
int width, height;
10821088
int[][] impl;
1089+
10831090
this(int w, int h)
10841091
{
10851092
width = w;
10861093
height = h;
1087-
impl = new int[w][h];
1094+
impl = new int[][](w, h);
10881095
}
10891096
int opIndex(size_t i1, size_t i2)
10901097
{
@@ -1099,7 +1106,7 @@ struct Rectangle
10991106
}
11001107
}
11011108

1102-
void test()
1109+
void main()
11031110
{
11041111
auto r = Rectangle(10,20);
11051112
int i = r[$-1, 0]; // same as: r.opIndex(r.opDollar!0, 0),
@@ -1108,6 +1115,7 @@ void test()
11081115
// which is r.opIndex(0, r.height-1)
11091116
}
11101117
------
1118+
)
11111119

11121120
$(P As the above example shows, a different compile-time argument is
11131121
passed to $(D opDollar) depending on which argument it appears in. A

0 commit comments

Comments
 (0)