Skip to content

Commit a532fc1

Browse files
authored
[spec/function] Improve nested function docs (#4330)
* [spec/function] Improve nested function docs Explain and simplify pure nested function example. Add *Nested Functions with Type Qualifiers* subheading under *Nested Functions*. * Rename Type Qualifiers heading to Method Attributes This includes e.g. `scope`, which can apply to a nested function. * Add link from `spec/const3.dd`
1 parent 243dfc0 commit a532fc1

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

spec/const3.dd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ struct S
393393
}
394394
---
395395

396+
$(P **See also:** $(DDSUBLINK spec/function, nested-qualifiers,
397+
Nested Functions with Method Attributes).)
398+
396399

397400
$(H2 $(LNAME2 const_type, Const Type))
398401

spec/function.dd

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -547,14 +547,19 @@ $(H4 $(LNAME2 pure-debug, Debugging))
547547
}
548548
---
549549

550-
$(H4 $(LNAME2 pure-nested, Nested Functions))
550+
$(H3 $(LNAME2 pure-nested, Nested Functions))
551551

552552
$(P $(RELATIVE_LINK2 nested, Nested functions) inside a pure function are implicitly marked as pure.)
553553

554+
$(P A non-static pure nested function can access (and sometimes mutate) data
555+
through its context pointer:)
556+
554557
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
555558
---
556-
pure int foo(int x, immutable int y)
559+
pure int foo()
557560
{
561+
int x = 5;
562+
558563
int bar()
559564
// implicitly marked as pure, to be "weakly pure"
560565
// since hidden context pointer to foo stack context is mutable
@@ -565,21 +570,18 @@ $(H4 $(LNAME2 pure-nested, Nested Functions))
565570
}
566571
pragma(msg, typeof(&bar)); // int delegate() pure
567572

568-
int baz() immutable
569-
// qualifies hidden context pointer with immutable,
570-
// and has no other parameters, therefore "strongly pure"
571-
{
572-
//return x; // error, cannot access mutable data
573-
// through the immutable context pointer
574-
return y; // ok
575-
}
576-
577-
// can call pure nested functions
578-
return bar() + baz();
573+
bar();
574+
return x;
579575
}
576+
577+
static assert(foo() == 10);
580578
---
581579
)
582580

581+
$(NOTE $(RELATIVE_LINK2 nested-qualifiers, Qualifying) a nested function
582+
as `immutable` prevents access to mutable data through its context pointer.
583+
An immutable pure nested function is strongly pure.)
584+
583585
$(H3 $(LNAME2 pure-factory-functions, Pure Factory Functions))
584586

585587
$(P A $(I pure factory function) is a strongly pure function
@@ -3147,6 +3149,33 @@ struct Foo
31473149
$(P Nested functions always have the D function linkage type.
31483150
)
31493151

3152+
$(H3 $(LNAME2 nested-qualifiers, Nested Functions with Method Attributes))
3153+
3154+
$(P A non-static nested function can have $(GLINK MemberFunctionAttributes).
3155+
Method attributes (other than the normal $(GLINK FunctionAttributes)) affect
3156+
the context pointer:)
3157+
3158+
$(SPEC_RUNNABLE_EXAMPLE_RUN
3159+
---
3160+
void test(int x, immutable int y)
3161+
{
3162+
int foo() // hidden context pointer is mutable
3163+
{
3164+
x = 10; // can access data in enclosing scope
3165+
// through the mutable context pointer
3166+
return x;
3167+
}
3168+
3169+
int bar() immutable // hidden context pointer is immutable
3170+
{
3171+
//return x; // error, cannot access nested data
3172+
// through the immutable context pointer
3173+
return y; // OK, `y` is immutable
3174+
}
3175+
}
3176+
---
3177+
)
3178+
31503179
$(H3 $(LNAME2 nested-declaration-order, Declaration Order))
31513180

31523181
$(P Unlike module level declarations, declarations within function

0 commit comments

Comments
 (0)