Goto considered awesome: Goto in Scala using delimited continuations(blog.richdougherty.com)
blog.richdougherty.com
Goto considered awesome: Goto in Scala using delimited continuations
http://blog.richdougherty.com/2009/03/goto-in-scala.html
3 comments
That's a fantastic resource! I've been fascinated with continuations recently because of just what you cover: they are an tool that can be used to implement many higher-level tools, like exceptions, generators, and cooperative threads. Things like this get much more clear when you see how closely they are related.
I am curious about your assertion that "with macros, it is not difficult to simulate preemptive multithreading". How would one go about doing this safely?
(The font changes partway through, btw. Maybe you forgot to close a tag?)
I am curious about your assertion that "with macros, it is not difficult to simulate preemptive multithreading". How would one go about doing this safely?
(The font changes partway through, btw. Maybe you forgot to close a tag?)
Good question.
In short, you can use the macros to hide the context-switching (yielding).
You could, for instance, force every function to perform a context switch right after its called. Since languages like Scheme encode even loops with function call, you're guaranteed to eventually hit a switch point. (And, if you allow other looping constructs, you could use macros to hide a yield inside them as well.)
In short, you can use the macros to hide the context-switching (yielding).
You could, for instance, force every function to perform a context switch right after its called. Since languages like Scheme encode even loops with function call, you're guaranteed to eventually hit a switch point. (And, if you allow other looping constructs, you could use macros to hide a yield inside them as well.)
Delimited continuations are a powerful new control flow construct that's available starting in Scala 2.8. In addition to useful applications like automatically transforming blocking, synchronous code into asynchronous code, it can also be abused to add in the tragically missing goto statement (in a limited form) to the language.
This clever implementation also uses exceptions for flow control. If you examine the debugging output from the continuations compiler plugin, you can see how this implemented. Basically, code around a 'shift' statement is segmented into two functions--one before the shift and one after. The one after is turned into a callback from the first.
This clever implementation also uses exceptions for flow control. If you examine the debugging output from the continuations compiler plugin, you can see how this implemented. Basically, code around a 'shift' statement is segmented into two functions--one before the shift and one after. The one after is turned into a callback from the first.
[deleted]
I realized when going over that material, however, that most students didn't know what a continuation was, or how to use them, so I created a "by-example" tutorial:
http://matt.might.net/articles/programming-with-continuation...
It covers exceptions, backtracking-search, the magic "amb" function, magic sat-solving, generators and cooperative threads.