Skip to content

Commit 5cfeb1b

Browse files
committed
add __traits(child, parent, member)
1 parent 18d159a commit 5cfeb1b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

spec/traits.dd

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ $(GNAME TraitsKeyword):
6666
$(GLINK getVirtualMethods)
6767
$(GLINK getUnitTests)
6868
$(GLINK parent)
69+
$(GLINK child)
6970
$(GLINK classInstanceSize)
7071
$(GLINK getVirtualIndex)
7172
$(GLINK allMembers)
@@ -1326,6 +1327,54 @@ $(H2 $(GNAME parent))
13261327
The result is the symbol that is the parent of it.
13271328
)
13281329

1330+
$(H2 $(GNAME child))
1331+
1332+
$(P Takes two arguments.
1333+
The first must be a symbol or expression.
1334+
The second is a symbol, such as an alias to a member of the first
1335+
argument.
1336+
The result is the second argument interpreted with its $(D this)
1337+
context set to the value of the first argument.
1338+
)
1339+
1340+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1341+
---
1342+
import std.stdio;
1343+
1344+
struct A
1345+
{
1346+
int i;
1347+
int foo(int j) {
1348+
return i * j;
1349+
}
1350+
T bar(T)(T t) {
1351+
return i + t;
1352+
}
1353+
}
1354+
1355+
alias Ai = A.i;
1356+
alias Abar = A.bar!int;
1357+
1358+
void main()
1359+
{
1360+
A a;
1361+
1362+
__traits(child, a, Ai) = 3;
1363+
writeln(a.i);
1364+
writeln(__traits(child, a, A.foo)(2));
1365+
writeln(__traits(child, a, Abar)(5));
1366+
}
1367+
---
1368+
)
1369+
1370+
Prints:
1371+
1372+
$(CONSOLE
1373+
3
1374+
6
1375+
7
1376+
)
1377+
13291378
$(H2 $(GNAME classInstanceSize))
13301379

13311380
$(P Takes a single argument, which must evaluate to either

0 commit comments

Comments
 (0)