Skip to content

Commit 05b1288

Browse files
ntreldlang-bot
authored andcommitted
[spec/class] Tweak alias this docs
Make examples runnable. Split up long example.
1 parent 3add73c commit 05b1288

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

spec/class.dd

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ $(GNAME AliasThis):
10351035
member.
10361036
)
10371037

1038+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10381039
---
10391040
struct S
10401041
{
@@ -1044,22 +1045,29 @@ struct S
10441045

10451046
int foo(int i) { return i * 2; }
10461047

1047-
void test()
1048+
void main()
10481049
{
10491050
S s;
10501051
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);
10551060
i = foo(s); // implicit conversion to int
1061+
assert(i == 14);
10561062
}
10571063
---
1064+
)
10581065

10591066
$(P If the member is a class or struct, undefined lookups will
10601067
be forwarded to the $(I AliasThis) member.
10611068
)
10621069

1070+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10631071
---
10641072
struct Foo
10651073
{
@@ -1073,19 +1081,23 @@ class Bar
10731081
alias foo this;
10741082
}
10751083

1076-
void test()
1084+
void main()
10771085
{
10781086
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);
10811091
}
10821092
---
1093+
)
10831094

10841095
$(P If the $(I Identifier) refers to a property member
10851096
function with no parameters, conversions and undefined
10861097
lookups are forwarded to the return value of the function.
10871098
)
10881099

1100+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10891101
---
10901102
struct S
10911103
{
@@ -1097,20 +1109,24 @@ struct S
10971109
alias get this;
10981110
}
10991111

1100-
void test()
1112+
void main()
11011113
{
11021114
S s;
11031115
s.x = 2;
1104-
int i = s; // i == 4
1116+
int i = s;
1117+
assert(i == 4);
11051118
}
11061119
---
1120+
)
1121+
11071122
$(P If an aggregate declaration defines an $(D opCmp) or $(D opEquals)
11081123
method, it will take precedence to that of the aliased this member. Note
11091124
that, unlike an $(D opCmp) method, an $(D opEquals) method is implicitly
11101125
defined for a $(D struct) declaration if a user defined one isn't provided;
11111126
this means that if the aliased this member $(D opEquals) is preferred it
11121127
should be explicitly defined:
11131128
)
1129+
$(SPEC_RUNNABLE_EXAMPLE_RUN
11141130
---
11151131
struct S
11161132
{
@@ -1128,6 +1144,21 @@ struct T
11281144
alias s this;
11291145
}
11301146

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+
---
11311162
struct U
11321163
{
11331164
int a;
@@ -1146,24 +1177,19 @@ struct V
11461177

11471178
void main()
11481179
{
1149-
S s1, s2;
1150-
T t1, t2;
11511180
U u1, u2;
11521181
V v1, v2;
11531182

1154-
assert(s1 == s2); // calls S.opEquals
1155-
assert(t1 == t2); // calls compiler generated T.opEquals that implements member-wise equality
11561183
assert(!(u1 < u2)); // calls U.opCmp
11571184
assert(!(v1 < v2)); // calls U.opCmp because V does not define an opCmp method
11581185
// so the alias this of v1 is employed; U.opCmp expects a
11591186
// paramter of type U, so alias this of v2 is used
11601187

1161-
assert(s1 == t1); // calls s1.opEquals(t1.s);
1162-
assert(t1 == s1); // calls t1.s.opEquals(s1);
11631188
assert(!(u1 < v1)); // calls u1.opCmp(v1.u);
11641189
assert(!(v1 < u1)); // calls v1.u.opCmp(v1);
11651190
}
11661191
---
1192+
)
11671193
$(P $(GLINK2 attribute, Attribute)s are ignored for $(D AliasThis).
11681194
)
11691195

0 commit comments

Comments
 (0)