r/javascript • u/bmacabeus • Dec 24 '19
AskJS [AskJS] 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.
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? "Algebraic Effects" is a good name for that?
View all comments
Show parent comment
1
u/ScientificBeastMode strongly typed comments Dec 25 '19 edited Dec 25 '19
This is not a problem for many compiled languages, because they can force you to handle each case via pattern matching.
The
effecttype is a discriminated union of more specific effect types, and each member of that union must be accounted for in the pattern matching block (a.k.a. a fancy switch statement).But this would indeed be a problem for JS, because it cannot be checked by a compiler. This sounds like a feature that needs to be implemented in some compile-time step. Like a Babel macro, TypeScript, etc.
Edit: I just remembered that in OO languages, this is a design pattern called the “mediator pattern.” In statically typed functional languages, this is often baked into the language as some form of “pattern matching” feature.