@@ -717,15 +717,13 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
717
717
718
718
int opApply(scope int delegate(ref uint) dg)
719
719
{
720
- int result = 0;
721
-
722
720
foreach (e; array)
723
721
{
724
- result = dg(e);
722
+ int result = dg(e);
725
723
if (result)
726
- break ;
724
+ return result ;
727
725
}
728
- return result ;
726
+ return 0 ;
729
727
}
730
728
}
731
729
@@ -751,26 +749,24 @@ $(CONSOLE
751
749
82
752
750
)
753
751
$(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
756
754
be allocated for it on the heap instead of the stack.
757
755
)
758
756
759
757
$(BEST_PRACTICE Annotate delegate parameters to `opApply` functions with `scope` when possible.)
760
758
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
763
761
exceptions thrown from a `foreach` body to both terminate the loop, and propagate outside
764
762
the `foreach` body.
765
763
)
766
764
767
765
$(H4 $(LNAME2 template-op-apply, Template `opApply`))
768
766
769
- $(P $(I opApply) can also be a templated function,
767
+ $(P $(D opApply) can also be a templated function,
770
768
which will infer the types of parameters based on the $(I ForeachStatement).
771
- )
772
-
773
- $(P For example:)
769
+ For example:)
774
770
775
771
$(SPEC_RUNNABLE_EXAMPLE_RUN
776
772
--------------
0 commit comments