Skip to content

Commit a0a0b57

Browse files
authored
[spec/function] Make 3 pure examples runnable (#3549)
* [spec/function] Make pure examples runnable Note: reading `*pz` is not an error, removed. Add subheading 'Strong vs Weak Purity' with anchor. Fix wrong return type of foo. * Add subheadings for debug and nested functions
1 parent c1287d6 commit a0a0b57

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

spec/function.dd

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,27 +407,29 @@ $(H2 $(LNAME2 pure-functions, Pure Functions))
407407
$(LI Modify the local state of the function.)
408408
$(LI Throw exceptions.)
409409
)
410+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
410411
---
411412
int x;
412413
immutable int y;
413-
const int* pz;
414414

415415
pure int foo(int i)
416416
{
417417
i++; // ok, modifying local state
418-
x = i; // error, modifying global state
419-
i = x; // error, reading mutable global state
418+
//x = i; // error, modifying global state
419+
//i = x; // error, reading mutable global state
420420
i = y; // ok, reading immutable global state
421-
i = *pz; // error, reading const global state
422421
throw new Exception("failed"); // ok
423422
}
424423
---
424+
)
425425

426426
$(P A pure function can override an impure function,
427427
but cannot be overridden by an impure function.
428428
I.e. it is covariant with an impure function.
429429
)
430430

431+
$(H3 $(LNAME2 weak-purity, Strong vs Weak Purity))
432+
431433
$(P A $(I weakly pure function) has parameters with mutable indirections.
432434
Program state can be modified transitively through the matching
433435
argument.
@@ -450,17 +452,19 @@ $(H2 $(LNAME2 pure-functions, Pure Functions))
450452
and cannot modify any program state external to the function.
451453
)
452454

455+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
453456
---
454457
struct S { double x; }
455458

456-
pure int foo(immutable(int)[] arr, int num, S val)
459+
pure size_t foo(immutable(int)[] arr, int num, S val)
457460
{
458461
//arr[num] = 1; // compile error
459-
num = 2; // has no side effect to the caller side
462+
num = 2; // has no side effect on the caller side
460463
val.x = 3.14; // ditto
461464
return arr.length;
462465
}
463466
---
467+
)
464468

465469
$(P A strongly pure function can call a weakly pure function.)
466470

@@ -478,6 +482,8 @@ $(H3 $(LNAME2 pure-special-cases, Special Cases))
478482
initial state upon function exit. It is the programmer's responsibility
479483
to ensure this. Setting these flags is not allowed in `@safe` code.)
480484

485+
$(H4 $(LNAME2 pure-debug, Debugging))
486+
481487
$(P A pure function can perform impure operations in statements that are in a
482488
$(GLINK2 version, ConditionalStatement)
483489
controlled by a $(GLINK2 version, DebugCondition).
@@ -494,8 +500,11 @@ $(H3 $(LNAME2 pure-special-cases, Special Cases))
494500
}
495501
---
496502

503+
$(H4 $(LNAME2 pure-nested, Nested Functions))
504+
497505
$(P $(RELATIVE_LINK2 nested, Nested functions) inside a pure function are implicitly marked as pure.)
498506

507+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
499508
---
500509
pure int foo(int x, immutable int y)
501510
{
@@ -522,6 +531,7 @@ $(H3 $(LNAME2 pure-special-cases, Special Cases))
522531
return bar() + baz();
523532
}
524533
---
534+
)
525535

526536
$(H3 $(LNAME2 pure-factory-functions, Pure Factory Functions))
527537

0 commit comments

Comments
 (0)