Skip to content

Algebraic Effect handlers #2567

Open
Open
@gisborne

Description

@gisborne

Algebraic effect handlers are about to land in OCaml. There are a number of other languages that implement them, including at least one that compiles to Javascript (called Koka — that it’s implementable in Javascript is I’m sure important!).

An algebraic effect handler is basically an exception that carries a continuation so you can resume the original computation. Kind of a strongly typed LISP condition.

Given this (simple!) language feature, one can implement in a very straightforward way:

  • exceptions/try/catch
  • async/await (without different colored functions/scattering async/await all over)
  • generators
  • streams
  • coroutines

But the feature is generally helpful to the developer also (eg anywhere you have to pass an argument through a bunch of functions so it can be used later on — well, you don’t have to do that; it opens up this and a bunch more new delightful coding patterns in fact).

It’s simple to implement and efficient (the compiler should try to work out the simplest way of doing the thing; if the effect handler doesn’t call out to anything, you can just resolve its logic and then continue; worst case, you block copy the stack frames from the caller to the handler to somewhere else, and then restore them by just copying the stack frames back).

Here is a nice, simple basic description of the feature:

https://overreacted.io/algebraic-effects-for-the-rest-of-us/

Here is a really nice half-hour video by the guy who did the language that compiles to Javascript:

https://www.youtube.com/watch?v=hrBq8R_kxI0

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions