@@ -1426,22 +1426,20 @@ void main()
1426
1426
1427
1427
$(H3 $(GNAME getOverloads))
1428
1428
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.
1436
1436
1437
1437
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1438
1438
---
1439
1439
import std.stdio;
1440
1440
1441
1441
class D
1442
1442
{
1443
- this() { }
1444
- ~this() { }
1445
1443
void foo() { }
1446
1444
int foo(int) { return 2; }
1447
1445
void bar(T)() { return T.init; }
@@ -1452,29 +1450,35 @@ void main()
1452
1450
{
1453
1451
D d = new D();
1454
1452
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)));
1457
1456
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 ));
1461
1460
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);
1464
1468
1465
- foreach (t ; __traits(getOverloads, D, "bar", true))
1466
- writeln(t .stringof);
1469
+ foreach (o ; __traits(getOverloads, D, "bar", true))
1470
+ writeln(o .stringof);
1467
1471
}
1468
1472
---
1469
1473
)
1470
1474
1471
1475
Prints:
1472
1476
1473
1477
$(CONSOLE
1474
- void()
1475
- int( )
1476
- void()
1477
- int( )
1478
+ void function ()
1479
+ int function(int )
1480
+ void function ()
1481
+ int function(int )
1478
1482
2
1479
1483
bar(T)()
1480
1484
bar(int n)
0 commit comments