Follow

little fennel coroutine control flow sketch. a niche but critical feature of my coroutine systems in JS and C# are the ability to "clean up" a "cancelled" coroutine. i accomplish this with try/finally in those languages, but you can do it in fennel too with macros, compile time scope tables, and lua's goto.

the cleanup macro takes two forms, one to "try" and one to "cleanup" in case the containing coroutine is cancelled by resuming with :cancel. wait is a replacement for yield.

· · Web · 2 · 0 · 1

my original implementation of a coroutine system in js did not support clean up. i added it on a gig because we needed to run a bunch of coroutines in parallel until one completed, after which we cancel the remaining coroutines. in many cases the cancelled coroutines needed to fade out an animation or, more critically, return a finite resource to a resource pool. its the kind of thing you dont need 90% of the time, but the remaining 10% is life or death.

js supports this pretty directly. you resume generators with .next() and there are .return() and .throw() methods that simulate a return or throw statement at the site of the matching yield. try/finally works as expected, and finally clauses are called correctly, which is where i put clean up code.

you can see my library directly explicitly .return() to clean up remaining coroutines in the `first` combinator here: github.com/nasser/ajeeb/blob/m

ultimately id want the syntax to be more like this, maybe try/finally arent the right words though

try/finally def aren't the right words because that implies try/catch/finally semantics which this does not have. it won't run finally code when an error is raised. that's doable but you need to use pcall, which means you need to wrap the try code in a function, which has a runtime cost, and im frankly not too worried about/interested in that use case.

@nasser ohhhh you're setting arbitrary new fields on the scope table altogether instead of just reading existing scope data! neat. I hadn't thought of that as a possibility but I dig it. (also a lot less likely to break due to compiler internal changes, haha)

@technomancy original implementation had me maintaining an external stack of labels before i realized that that was the compiler's job and y'all did a good job of exposing nested scope data to macro nerds like me 🙏

Sign in to participate in the conversation
Merveilles

Revel in the marvels of the universe. We are a collective of forward-thinking individuals who strive to better ourselves and our surroundings through constant creation. We express ourselves through music, art, games, and writing. We also put great value in play. A warm welcome to any like-minded people who feel these ideals resonate with them.