Statement "goto" between functions

Dennis dkorpel at gmail.com
Sat May 25 20:18:23 UTC 2019


On Saturday, 25 May 2019 at 19:29:03 UTC, Andrey wrote:
> 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.

D being a systems programming language allows basically anything 
you can do with assembly hacks. Here is a posix-only demo using 
the C standard library functions setjmp (analogous to try-catch) 
and longjmp (analogous to throw).

https://run.dlang.io/is/aPlxfQ

As you can see it is still pretty ugly, and it's absolutely not 
recommended to use this.

> 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.
> ...
> What do you think?

I think you should either:
- rethink your design
- check return values manually
- still use Exceptions, which is the only way of jumping over 
functions that is supported by the language.

A long-jump solution is bad because:

- your code's control flow becomes spaghetti
- things like destructors, Exceptions and scope statements won't 
function properly anymore.
- setjmp/longjmp save/restore all registers, so it's still slower 
than a goto
- while I can't speak from experience (I haven't used them in 
practice), I bet they easily break, are hard to debug and are not 
very portable.



More information about the Digitalmars-d mailing list