@@ -345,7 +345,7 @@ T opBinary(string op)(T rhs)
345
345
{
346
346
static if (op == "+") return data + rhs.data;
347
347
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");
349
349
}
350
350
---
351
351
@@ -354,7 +354,7 @@ T opBinary(string op)(T rhs)
354
354
---
355
355
T opBinary(string op)(T rhs)
356
356
{
357
- return mixin("data "~op~ " rhs.data");
357
+ return mixin("data " ~ op ~ " rhs.data");
358
358
}
359
359
---
360
360
@@ -607,14 +607,15 @@ $(H2 $(LEGACY_LNAME2 FunctionCall, function-call, Function Call Operator Overloa
607
607
declaring a function named $(CODE opCall):
608
608
)
609
609
610
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
610
611
-------
611
612
struct F
612
613
{
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);
615
616
}
616
617
617
- void test ()
618
+ void main ()
618
619
{
619
620
F f;
620
621
int i;
@@ -623,6 +624,7 @@ $(H2 $(LEGACY_LNAME2 FunctionCall, function-call, Function Call Operator Overloa
623
624
i = f(3,4,5); // same as i = f.opCall(3,4,5);
624
625
}
625
626
-------
627
+ )
626
628
627
629
$(P In this way a struct or class object can behave as if it
628
630
were a function.
@@ -660,17 +662,20 @@ $(H3 $(LNAME2 static-opcall, Static opCall))
660
662
type names.
661
663
)
662
664
665
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
663
666
-------
664
667
struct Double
665
668
{
666
- $(CODE_HIGHLIGHT static) int $(CODE_HIGHLIGHT opCall) (int x) { return x * 2; }
669
+ static int opCall(int x) { return x * 2; }
667
670
}
668
- void test()
671
+
672
+ void main()
669
673
{
670
674
int i = Double(2);
671
675
assert(i == 4);
672
676
}
673
677
-------
678
+ )
674
679
675
680
$(P Mixing struct constructors and $(D static opCall) is not allowed.)
676
681
@@ -971,7 +976,7 @@ struct S
971
976
void main()
972
977
{
973
978
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))
975
980
assert(t == [1, 2]);
976
981
}
977
982
---
@@ -1075,16 +1080,18 @@ $(H3 $(LEGACY_LNAME2 Dollar, dollar, Dollar Operator Overloading))
1075
1080
understood by $(D opSlice) and $(D opIndex).
1076
1081
)
1077
1082
1083
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1078
1084
------
1079
1085
struct Rectangle
1080
1086
{
1081
1087
int width, height;
1082
1088
int[][] impl;
1089
+
1083
1090
this(int w, int h)
1084
1091
{
1085
1092
width = w;
1086
1093
height = h;
1087
- impl = new int[w][h] ;
1094
+ impl = new int[][](w, h) ;
1088
1095
}
1089
1096
int opIndex(size_t i1, size_t i2)
1090
1097
{
@@ -1099,7 +1106,7 @@ struct Rectangle
1099
1106
}
1100
1107
}
1101
1108
1102
- void test ()
1109
+ void main ()
1103
1110
{
1104
1111
auto r = Rectangle(10,20);
1105
1112
int i = r[$-1, 0]; // same as: r.opIndex(r.opDollar!0, 0),
@@ -1108,6 +1115,7 @@ void test()
1108
1115
// which is r.opIndex(0, r.height-1)
1109
1116
}
1110
1117
------
1118
+ )
1111
1119
1112
1120
$(P As the above example shows, a different compile-time argument is
1113
1121
passed to $(D opDollar) depending on which argument it appears in. A
0 commit comments