opApply Magic Function Body Transformation

Nick Treleaven nick at geany.org
Fri Aug 1 10:54:42 UTC 2025


On Monday, 28 July 2025 at 12:25:39 UTC, kinke wrote:
> then the rewrite becomes a bit more complex:
> ```d
> int __result;  // magic variable inserted by the compiler, for 
> the main() return value
> const __opApplyResult = ints.opApply((ref int item) {
>   if (item == 0) {
>     __result = item;  // set return value for parent function
>     return 2;  // return => abort the loop and exit from parent 
> function
>   }
>   if (item == 1)
>     return 0;  // continue => abort this iteration
>   if (item == 2)
>     return 1;  // break => abort this and all future iterations
>   printf("%d\n", item);
>   return 0;    // continue with next iteration
> });
> switch (__opApplyResult) {
>   default:
>     break;
>   case 2:
>     return __result;
> }
> return __result = 0;
> ```

Interesting, thanks. How does it work if there's a `goto` in the 
foreach body with a label outside the foreach?


More information about the Digitalmars-d-learn mailing list