3 ms·
> Languages other than C give you options for flow control so that you don't need goto for that. The idiom `if (error) goto cleanup` is about the only thing I
by lelanthran 3y ago
> Languages other than C give you options for flow control so that you don't need goto for that.
The idiom `if (error) goto cleanup` is about the only thing I see goto used for. What flow control replaces that other than exceptions?
- randomdata 3y ago> What flow control replaces that other than exceptions? defer has gained in popularity for that situation.
- sirwhinesalot 3y agoJumping out of nested loops. Implementing higher level constructs like yield or defer. State machines. Compiler output that uses C as a "cross-platform" assembly language. All of them are better served with more specialized language constructs but as a widely applicable hammer goto is pretty nice. I don't expect C to have good error handling or generators any time soon but with goto I can deal with it.
- lelanthran 3y agoI'm actually familiar with this, having used libprotothreads in production for about 4 years. Something like libprotothreads can't actually be implemented in a language that doesn't have gotos, so yeah, I see the need for it.
- nickpsecurity 3y agoCompiling HLL constructs in some of those scenarios ultimately produces a jump statement. So, it makes sense that a higher-level version of a jump would be helpful in the same situations.
- cozzyd 3y agoRAII + destructors Though gcc supports cleanup functions, just not very ergonomically.