Skip to content

Commit 67f7859

Browse files
committed
[spec/template] Improve nested template docs
Make examples runnable. Tweak wording. Document that templates inside a struct can't introduce non-static fields. Extend method template example.
1 parent e1d492b commit 67f7859

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

spec/template.dd

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,7 @@ $(H2 $(LNAME2 nested-templates, Nested Templates))
14711471
instantiated functions will implicitly capture the context of the
14721472
enclosing scope.)
14731473

1474+
$(SPEC_RUNNABLE_EXAMPLE_RUN
14741475
----
14751476
class C
14761477
{
@@ -1502,30 +1503,45 @@ $(H2 $(LNAME2 nested-templates, Nested Templates))
15021503
assert(c.num == 10);
15031504
}
15041505
----
1506+
)
15051507

1506-
$(P Above, $(D Foo!().foo) will work just the same as a member function
1508+
$(P Above, $(D Foo!().foo) will work just the same as a `final` member function
15071509
of class $(D C), and $(D Bar!().bar) will work just the same as a nested
15081510
function within function $(D main$(LPAREN)$(RPAREN)).)
15091511

1510-
$(H3 $(LNAME2 limitations, Limitations))
1512+
$(H3 $(LNAME2 limitations, Aggregate Type Limitations))
15111513

1512-
$(P Templates cannot be used to add non-static fields or
1513-
virtual functions to classes or interfaces.
1514-
For example:)
1514+
$(P A nested template cannot add non-static fields to an aggregate type.
1515+
Fields declared in a nested template will be implicitly `static`.)
1516+
$(P A nested template cannot add virtual functions to a class or interface.
1517+
Methods inside a nested template will be implicitly `final`.)
15151518

1519+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
15161520
------
15171521
class Foo
15181522
{
15191523
template TBar(T)
15201524
{
1521-
T xx; // becomes a static field of Foo
1522-
int func(T) { ... } // non-virtual
1525+
T xx; // becomes a static field of Foo
1526+
void func(T) {} // implicitly final
1527+
//abstract void baz(); // error, final functions cannot be abstract
15231528

1524-
static T yy; // Ok
1525-
static int func(T t, int y) { ... } // Ok
1529+
static T yy; // Ok
1530+
static void func(T t, int y) {} // Ok
15261531
}
15271532
}
1533+
1534+
void main()
1535+
{
1536+
alias bar = Foo.TBar!int;
1537+
bar.xx++;
1538+
//bar.func(1); // error, no this
1539+
1540+
auto o = new Foo;
1541+
o.TBar!int.func(1); // OK
1542+
}
15281543
------
1544+
)
15291545

15301546
$(H3 $(LNAME2 implicit-nesting, Implicit Nesting))
15311547

0 commit comments

Comments
 (0)