Statement "goto" between functions

Andrey saasecondbox at yandex.ru
Sat May 25 19:29:03 UTC 2019


Hello,
Can anybody say - is it possible to implement in D a statement 
"goto between functions". Not between random functions but 
between functions that are currently on the stack. Such "goto" 
will be something like labeled break in nested loops.
Example of what I mean. Lets imagine call stack:
> int main			
> someFunction1 	
> someFunction2
> ... someFunctionN
and code:
> void someFunction1(T...)(T args)
> {
>     // do some actions...
>     someFunction2(10, true, "hello"); // nested call
>     // do some actions...
>     GOTO_LABEL_EXTRA1:
>     writeln("Exit - GOTO_LABEL_EXTRA1 (or regular exit)");
>     return;
> 
>     GOTO_LABEL_EXTRA2:
>     writeln("Exit - GOTO_LABEL_EXTRA2");
> 
>     // do other actions
> }
> void someFunctionN()
> {
>     // do some actions...
>     if(condition) goto GOTO_LABEL_EXTRA1;
>     // do other actions...
>     if(othercondition) goto GOTO_LABEL_EXTRA2;
>     // do some other actions...
>     return; // regular exit
> }

Inside "main" we call "someFunction1" where we call 
"someFunction2" where we call "someFunction3" ... where we call 
"someFunctionN". This looks like nested loops.
When we make some calculations inside "someFunctionN" we exit it. 
Stack is unrolled and we are again inside "someFunction1". 
Execution continues from label (if exit was through goto) or 
through regular flow.
At the moment there are (as I understand) two different and 
non-overlapping ways to exit from function - usual "return" or 
exception. Exceptions are expansive and aren't suited for normal 
flow. Returns are ambiguous. To get rid of ambiguity one can 
return bool flag and then check it. But it is ugly and why I must 
check 10 times the same flag when in someFunctionN I know exactly 
result and currect branch of execution flow? I want just to jump 
to next instruction (and free stack and heap) without any 
additional runtime things.

And for loops there are (as I understand) four ways in exit - 
return, exception, break and goto.

This example we can see from another side - as if we mixin code 
of someFunction2 .. someFunctionN in someFunction1.

What do you think?


More information about the Digitalmars-d mailing list