Ask HN: JavaScript Proposal – Algebraic Effects?
Hey guys, I'm writing a Babel plugin to be able to use algebraic effects in JS https://github.com/macabeus/js-proposal-algebraic-effects
No more function color! Yes one-shot delimited continuation!
What the hell?! Well... I really recommend that you read this blog post by Dan Abramov explaining algebraic effects - and how it could be very useful on our JavaScript code: https://overreacted.io/algebraic-effects-for-the-rest-of-us/
This project is a runnable POC with a Babel's "plugin", so you could write some code and taste this new concept in JavaScript. Its features, syntax, and goals are very inspired by Dan Abramov's blog post mentioned above. In short, with algebraic effects, you could separate what from the how and have fewer refactors.
What do you think? Would that be a good feature for JS? So instead of requiring the callee to be async or not you have to make sure your caller is executing in the right effect handler context? Sounds like we replaced the coloring problem with another one? I can see how it benefits the language designer because you could implement try catch and async with this but how does it benefit the user? Yeah. We need to ensure that we have a handler to catch the effect (there is an issue about that: https://github.com/macabeus/js-proposal-algebraic-effects/is...) For user, it could be useful to separate what from the how.
For example, we could have a function that launches effects to manipulate the file system, and for test/mock purpose we could catch this effects with handlers that mock the file system. I know that the pattern dependency injection solve the same problem, but with effects you could do it with a more natural code. Since JS lacks a static type system, isn't it a bit misleading to call these "algebraic" effects, when algebraic is referring to the type system? I'm still not clear where the "algebraic" part comes in here. Algebra lacks a static type system, too. If among the numbers we recognize types such as "integer", "rational", "real", "complex", we need dynamic typing, because the value of any polynomial fraction P(z)/Q(z) for any given z can be any of these types, depending nontrivially on the value of z and all the coefficients. Maybe the name "algebraic effect" isn't good enough... What name you think that could fit better? I think this is just a (limited) version of Lisp’s conditions and restarts (https://wiki.c2.com/?CommonLispConditionSystem, https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule). So, I would call these conditions and restarts.