@@ -1035,6 +1035,7 @@ $(GNAME AliasThis):
1035
1035
member.
1036
1036
)
1037
1037
1038
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1038
1039
---
1039
1040
struct S
1040
1041
{
@@ -1044,22 +1045,29 @@ struct S
1044
1045
1045
1046
int foo(int i) { return i * 2; }
1046
1047
1047
- void test ()
1048
+ void main ()
1048
1049
{
1049
1050
S s;
1050
1051
s.x = 7;
1051
- int i = -s; // i == -7
1052
- i = s + 8; // i == 15
1053
- i = s + s; // i == 14
1054
- i = 9 + s; // i == 16
1052
+ int i = -s;
1053
+ assert(i == -7);
1054
+ i = s + 8;
1055
+ assert(i == 15);
1056
+ i = s + s;
1057
+ assert(i == 14);
1058
+ i = 9 + s;
1059
+ assert(i == 16);
1055
1060
i = foo(s); // implicit conversion to int
1061
+ assert(i == 14);
1056
1062
}
1057
1063
---
1064
+ )
1058
1065
1059
1066
$(P If the member is a class or struct, undefined lookups will
1060
1067
be forwarded to the $(I AliasThis) member.
1061
1068
)
1062
1069
1070
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1063
1071
---
1064
1072
struct Foo
1065
1073
{
@@ -1073,19 +1081,23 @@ class Bar
1073
1081
alias foo this;
1074
1082
}
1075
1083
1076
- void test ()
1084
+ void main ()
1077
1085
{
1078
1086
auto bar = new Bar;
1079
- int i = bar.baz; // i == 4
1080
- i = bar.get(); // i == 7
1087
+ int i = bar.baz;
1088
+ assert(i == 4);
1089
+ i = bar.get();
1090
+ assert(i == 7);
1081
1091
}
1082
1092
---
1093
+ )
1083
1094
1084
1095
$(P If the $(I Identifier) refers to a property member
1085
1096
function with no parameters, conversions and undefined
1086
1097
lookups are forwarded to the return value of the function.
1087
1098
)
1088
1099
1100
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1089
1101
---
1090
1102
struct S
1091
1103
{
@@ -1097,20 +1109,24 @@ struct S
1097
1109
alias get this;
1098
1110
}
1099
1111
1100
- void test ()
1112
+ void main ()
1101
1113
{
1102
1114
S s;
1103
1115
s.x = 2;
1104
- int i = s; // i == 4
1116
+ int i = s;
1117
+ assert(i == 4);
1105
1118
}
1106
1119
---
1120
+ )
1121
+
1107
1122
$(P If an aggregate declaration defines an $(D opCmp) or $(D opEquals)
1108
1123
method, it will take precedence to that of the aliased this member. Note
1109
1124
that, unlike an $(D opCmp) method, an $(D opEquals) method is implicitly
1110
1125
defined for a $(D struct) declaration if a user defined one isn't provided;
1111
1126
this means that if the aliased this member $(D opEquals) is preferred it
1112
1127
should be explicitly defined:
1113
1128
)
1129
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1114
1130
---
1115
1131
struct S
1116
1132
{
@@ -1128,6 +1144,21 @@ struct T
1128
1144
alias s this;
1129
1145
}
1130
1146
1147
+ void main()
1148
+ {
1149
+ S s1, s2;
1150
+ T t1, t2;
1151
+
1152
+ assert(s1 == s2); // calls S.opEquals
1153
+ assert(t1 == t2); // calls compiler generated T.opEquals that implements member-wise equality
1154
+
1155
+ assert(s1 == t1); // calls s1.opEquals(t1.s);
1156
+ assert(t1 == s1); // calls t1.s.opEquals(s1);
1157
+ }
1158
+ ---
1159
+ )
1160
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1161
+ ---
1131
1162
struct U
1132
1163
{
1133
1164
int a;
@@ -1146,24 +1177,19 @@ struct V
1146
1177
1147
1178
void main()
1148
1179
{
1149
- S s1, s2;
1150
- T t1, t2;
1151
1180
U u1, u2;
1152
1181
V v1, v2;
1153
1182
1154
- assert(s1 == s2); // calls S.opEquals
1155
- assert(t1 == t2); // calls compiler generated T.opEquals that implements member-wise equality
1156
1183
assert(!(u1 < u2)); // calls U.opCmp
1157
1184
assert(!(v1 < v2)); // calls U.opCmp because V does not define an opCmp method
1158
1185
// so the alias this of v1 is employed; U.opCmp expects a
1159
1186
// paramter of type U, so alias this of v2 is used
1160
1187
1161
- assert(s1 == t1); // calls s1.opEquals(t1.s);
1162
- assert(t1 == s1); // calls t1.s.opEquals(s1);
1163
1188
assert(!(u1 < v1)); // calls u1.opCmp(v1.u);
1164
1189
assert(!(v1 < u1)); // calls v1.u.opCmp(v1);
1165
1190
}
1166
1191
---
1192
+ )
1167
1193
$(P $(GLINK2 attribute, Attribute)s are ignored for $(D AliasThis).
1168
1194
)
1169
1195
0 commit comments