@@ -407,27 +407,29 @@ $(H2 $(LNAME2 pure-functions, Pure Functions))
407
407
$(LI Modify the local state of the function.)
408
408
$(LI Throw exceptions.)
409
409
)
410
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
410
411
---
411
412
int x;
412
413
immutable int y;
413
- const int* pz;
414
414
415
415
pure int foo(int i)
416
416
{
417
417
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
420
420
i = y; // ok, reading immutable global state
421
- i = *pz; // error, reading const global state
422
421
throw new Exception("failed"); // ok
423
422
}
424
423
---
424
+ )
425
425
426
426
$(P A pure function can override an impure function,
427
427
but cannot be overridden by an impure function.
428
428
I.e. it is covariant with an impure function.
429
429
)
430
430
431
+ $(H3 $(LNAME2 weak-purity, Strong vs Weak Purity))
432
+
431
433
$(P A $(I weakly pure function) has parameters with mutable indirections.
432
434
Program state can be modified transitively through the matching
433
435
argument.
@@ -450,17 +452,19 @@ $(H2 $(LNAME2 pure-functions, Pure Functions))
450
452
and cannot modify any program state external to the function.
451
453
)
452
454
455
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
453
456
---
454
457
struct S { double x; }
455
458
456
- pure int foo(immutable(int)[] arr, int num, S val)
459
+ pure size_t foo(immutable(int)[] arr, int num, S val)
457
460
{
458
461
//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
460
463
val.x = 3.14; // ditto
461
464
return arr.length;
462
465
}
463
466
---
467
+ )
464
468
465
469
$(P A strongly pure function can call a weakly pure function.)
466
470
@@ -478,6 +482,8 @@ $(H3 $(LNAME2 pure-special-cases, Special Cases))
478
482
initial state upon function exit. It is the programmer's responsibility
479
483
to ensure this. Setting these flags is not allowed in `@safe` code.)
480
484
485
+ $(H4 $(LNAME2 pure-debug, Debugging))
486
+
481
487
$(P A pure function can perform impure operations in statements that are in a
482
488
$(GLINK2 version, ConditionalStatement)
483
489
controlled by a $(GLINK2 version, DebugCondition).
@@ -494,8 +500,11 @@ $(H3 $(LNAME2 pure-special-cases, Special Cases))
494
500
}
495
501
---
496
502
503
+ $(H4 $(LNAME2 pure-nested, Nested Functions))
504
+
497
505
$(P $(RELATIVE_LINK2 nested, Nested functions) inside a pure function are implicitly marked as pure.)
498
506
507
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
499
508
---
500
509
pure int foo(int x, immutable int y)
501
510
{
@@ -522,6 +531,7 @@ $(H3 $(LNAME2 pure-special-cases, Special Cases))
522
531
return bar() + baz();
523
532
}
524
533
---
534
+ )
525
535
526
536
$(H3 $(LNAME2 pure-factory-functions, Pure Factory Functions))
527
537
0 commit comments