Skip to content

Commit 9dcd605

Browse files
committed
Simplify example and improve formatting
1 parent 2e382b2 commit 9dcd605

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

spec/statement.dd

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,13 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
717717

718718
int opApply(scope int delegate(ref uint) dg)
719719
{
720-
int result = 0;
721-
722720
foreach (e; array)
723721
{
724-
result = dg(e);
722+
int result = dg(e);
725723
if (result)
726-
break;
724+
return result;
727725
}
728-
return result;
726+
return 0;
729727
}
730728
}
731729

@@ -751,26 +749,24 @@ $(CONSOLE
751749
82
752750
)
753751
$(P The `scope` storage class on the $(I dg) parameter means that the delegate does
754-
not escape the scope of the $(I opApply) function (an example would be assigning $(I dg) to a
755-
global). If it cannot be statically guaranteed that $(I dg) does not escape, a closure may
752+
not escape the scope of the $(D opApply) function (an example would be assigning $(I dg) to a
753+
global variable). If it cannot be statically guaranteed that $(I dg) does not escape, a closure may
756754
be allocated for it on the heap instead of the stack.
757755
)
758756

759757
$(BEST_PRACTICE Annotate delegate parameters to `opApply` functions with `scope` when possible.)
760758

761-
$(P It is important to make sure that, if $(D opApply) catches any exceptions, that those
762-
exceptions did not originate from the delegate passed to $(I opApply). The user would expect
759+
$(P $(B Important:) If $(D opApply) catches any exceptions, ensure that those
760+
exceptions did not originate from the delegate passed to $(D opApply). The user would expect
763761
exceptions thrown from a `foreach` body to both terminate the loop, and propagate outside
764762
the `foreach` body.
765763
)
766764

767765
$(H4 $(LNAME2 template-op-apply, Template `opApply`))
768766

769-
$(P $(I opApply) can also be a templated function,
767+
$(P $(D opApply) can also be a templated function,
770768
which will infer the types of parameters based on the $(I ForeachStatement).
771-
)
772-
773-
$(P For example:)
769+
For example:)
774770

775771
$(SPEC_RUNNABLE_EXAMPLE_RUN
776772
--------------

0 commit comments

Comments
 (0)