
Ask HN: JavaScript Proposal – Algebraic Effects? - macabeus
Hey guys, I&#x27;m writing a Babel plugin to be able to use algebraic effects in JS
https:&#x2F;&#x2F;github.com&#x2F;macabeus&#x2F;js-proposal-algebraic-effects<p>No more function color! Yes one-shot delimited continuation!<p>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:&#x2F;&#x2F;overreacted.io&#x2F;algebraic-effects-for-the-rest-of-us&#x2F;<p>This project is a runnable POC with a Babel&#x27;s &quot;plugin&quot;, so you could write some code and taste this new concept in JavaScript. Its features, syntax, and goals are very inspired by Dan Abramov&#x27;s blog post mentioned above. In short, with algebraic effects, you could separate what from the how and have fewer refactors.<p>What do you think? Would that be a good feature for JS?
======
haecceity
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?

~~~
macabeus
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...](https://github.com/macabeus/js-proposal-algebraic-
effects/issues/8))

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.

------
pickdenis
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.

~~~
macabeus
Maybe the name "algebraic effect" isn't good enough... What name you think
that could fit better?

~~~
Someone
I think this is just a (limited) version of Lisp’s conditions and restarts
([https://wiki.c2.com/?CommonLispConditionSystem](https://wiki.c2.com/?CommonLispConditionSystem),
[https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule](https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule)).

So, I would call these conditions and restarts.

