Skip to content

Commit caf9c49

Browse files
authored
[spec/traits] Improve getOverloads docs (#3892)
Use list, add link. Remove unneeded ctor, dtor. Tweak example, use fooOverloads alias. Add comments. Add example of getOverloads on a module. Fix writeln typeid method output.
1 parent 02664a1 commit caf9c49

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

spec/traits.dd

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,22 +1426,20 @@ void main()
14261426

14271427
$(H3 $(GNAME getOverloads))
14281428

1429-
$(P The first argument is an aggregate (e.g. struct/class/module).
1430-
The second argument is a `string` that matches the name of
1431-
the member(s) to return.
1432-
The third argument is a `bool`, and is optional. If `true`, the
1433-
result will also include template overloads.
1434-
The result is a symbol sequence of all the overloads of the supplied name.
1435-
)
1429+
* The first argument is an aggregate type or instance, or a module.
1430+
* The second argument is a `string` that matches the name of
1431+
the member(s) to return.
1432+
* The third argument is a `bool`, and is optional. If `true`, the
1433+
result will also include template overloads.
1434+
* The result is a $(DDSUBLINK spec/template, homogeneous_sequences, symbol sequence)
1435+
of all the overloads of the supplied name.
14361436

14371437
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
14381438
---
14391439
import std.stdio;
14401440

14411441
class D
14421442
{
1443-
this() { }
1444-
~this() { }
14451443
void foo() { }
14461444
int foo(int) { return 2; }
14471445
void bar(T)() { return T.init; }
@@ -1452,29 +1450,35 @@ void main()
14521450
{
14531451
D d = new D();
14541452

1455-
foreach (t; __traits(getOverloads, D, "foo"))
1456-
writeln(typeid(typeof(t)));
1453+
alias fooOverloads = __traits(getOverloads, D, "foo");
1454+
foreach (o; fooOverloads)
1455+
writeln(typeid(typeof(o)));
14571456

1458-
alias b = typeof(__traits(getOverloads, D, "foo"));
1459-
foreach (t; b)
1460-
writeln(typeid(t));
1457+
// typeof on a symbol sequence gives a type sequence
1458+
foreach (T; typeof(fooOverloads))
1459+
writeln(typeid(T));
14611460

1462-
auto i = __traits(getOverloads, d, "foo")[1](1);
1463-
writeln(i);
1461+
// calls d.foo(3)
1462+
auto i = __traits(getOverloads, d, "foo")[1](3);
1463+
assert(i == 2);
1464+
1465+
// pass true to include templates
1466+
// calls std.stdio.writeln(i)
1467+
__traits(getOverloads, std.stdio, "writeln", true)[0](i);
14641468

1465-
foreach (t; __traits(getOverloads, D, "bar", true))
1466-
writeln(t.stringof);
1469+
foreach (o; __traits(getOverloads, D, "bar", true))
1470+
writeln(o.stringof);
14671471
}
14681472
---
14691473
)
14701474

14711475
Prints:
14721476

14731477
$(CONSOLE
1474-
void()
1475-
int()
1476-
void()
1477-
int()
1478+
void function()
1479+
int function(int)
1480+
void function()
1481+
int function(int)
14781482
2
14791483
bar(T)()
14801484
bar(int n)

0 commit comments

Comments
 (0)