Hacker News new | ask | show | jobs
by kccqzy 861 days ago
I'm glad that this explanation involves a comparison with goto especially with a discussion of "goto considered harmful". But IMO excessively using continuations results in the same kind of spaghetti code as code that excessively uses goto. Delimited continuations, on the other hand, essentially places a restriction on where the continuation can return to. The analogy with using goto is that the target of the goto has to be within a well specified block of code. This restriction gets rid of the temptation by programmers to take shortcuts and write overly clever but unmaintainable code.
2 comments

Simply using simple tail calls is goto-like spaghetti code.

Any if/goto program graph or flowchart can be turned into tail calls that have the same shape. For each node in the goto graph, we can have a tail-called function, 1:1.

Does any language other than Common Lisp provide a "delimited goto"?
For many languages, "delimited goto" is approximated by the language's exception mechanism. You can raise an exception pretty much anywhere you want including in totally different functions many stack frames deep, and the control transfers to the closest catch handler.

Personally I'm not a fan because even the name "exception" carries some baggage. Many people think it should only be reserved for exceptional situations, not expected control flow, and some languages have penalties in performance when using them. That's why they tend not be used for vanilla control flow.