4 ms·
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?
- haecceity 7y agoSo 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 7y agoYeah. 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/issues/8 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.
- pickdenis 7y agoSince 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 7y agoMaybe the name "algebraic effect" isn't good enough... What name you think that could fit better?
- Someone 7y agoI 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.
- kazinator 7y agoAlgebra 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.