Skip to content

Commit c015dd8

Browse files
ntreldlang-bot
authored andcommitted
[spec/template.dd] Tweak 'This Parameter' examples
Make Addable example runnable & show that the this parameter is not the same as the runtime type.
1 parent e6ae930 commit c015dd8

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

spec/template.dd

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ $(GNAME TemplateThisParameter):
380380
---
381381
struct S
382382
{
383-
void foo(this T)(int i) const
383+
void foo(this T)() const
384384
{
385385
pragma(msg, T);
386386
}
@@ -389,11 +389,11 @@ $(GNAME TemplateThisParameter):
389389
void main()
390390
{
391391
const(S) s;
392-
(&s).foo(1);
392+
(&s).foo();
393393
S s2;
394-
s2.foo(2);
394+
s2.foo();
395395
immutable(S) s3;
396-
s3.foo(3);
396+
s3.foo();
397397
}
398398
---
399399
)
@@ -438,6 +438,7 @@ immutable(S)
438438
$(P Here the method $(D add) returns the base type, which doesn't implement the
439439
$(D remove) method. The $(D template this) parameter can be used for this purpose:)
440440

441+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
441442
---
442443
interface Addable(T)
443444
{
@@ -458,9 +459,15 @@ immutable(S)
458459
void main()
459460
{
460461
auto list = new List!int;
461-
list.add(1).remove(1); // ok
462+
static assert(is(typeof(list.add(1)) == List!int));
463+
list.add(1).remove(1); // ok, List.add
464+
465+
Addable!int a = list;
466+
// a.add calls Addable.add
467+
static assert(is(typeof(a.add(1)) == Addable!int));
462468
}
463469
---
470+
)
464471

465472
$(H2 $(LNAME2 template_value_parameter, Template Value Parameters))
466473

0 commit comments

Comments
 (0)