Emacs 26, Threads, and Generators

One of the things that a lot of people are exited about in Emacs 26 is the introduction of basic threading. Another, less mentioned, feature is generators. The excellent Chris Wellons has a must-read post that talks about both of these innovations.

He first considers generators, which he describes as mostly modeled after Python generators and, to a lesser extent, JavaScript generators. Generators return an iterator object that performs a calculation but can stop in middle of the operation to return a value. When it’s called again, it produces the next value and stops again. Python users will be familiar with the idea.

In Elisp, the generators are implemented as closures. Wellons describes how they work and some of the gotchas involved but if you’ve ever implemented a counter function that return the next sequential integer each time it’s called, you’ll be familiar with the concept.

Next Wellons talks about the threading implementation. He says that they’re basically pthreads and that they should be considered a first step in bringing threads to Emacs. Again, he discusses some of the things that can go wrong.

Finally, being Wellons, he wrote an implementation of generators using threads. It was just a proof-of-concept implementation meant to give him some experience in using the new facilities. You can take a look at the code on GitHub if you’re interested.

As always with Wellons’ posts, I learned some new things by reading it. If you do any Elisp at all, you should take a look.

This entry was posted in Programming and tagged . Bookmark the permalink.