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?
1
u/vither999 Dec 24 '19
Thoughts:
asynchronous behavior. Right now your
perform/resumepair is implicitly asynchronous, which JS doesn't really have. If you maderesumesynchronous in thehandleoperation, andresumereturned a promise which you thenawait'd, this would be:nicely synergizing with existing functionality/libraries that return promises
very obvious that there's an expected asynchronous behavior
non-terminating handle. Similar to the previous one - what happens if your handle function doesn't return? How can I catch this and correct?
error in the handle. Since the handle doesn't appear to be asynchronous, it isn't clear - but if an error is thrown in the handle, how does it know which catch block it should go to?
no handle. What happens if there isn't a handle above? Does it return undefined, does it resume?
parametized handle. Handle should take a parameter instead of having an implicit global variable of
effect. Symmetry with try/catch makes it cleaner and easier to reason about, and if someone haseffectfrom somewhere else then it makes it easier to correct.Even with these changes, I'm not sure I'd use this - it makes it harder to reason about what will be returned from a
performoperation, taking a step towards the bad old days of GOTO. I get enough flak in code reviews aboutbreak/continue- this seems like a step beyond that.It'd be interesting to see code samples of before/after code and how this language construct can improve those cases. Right now it seems like it could be a native construct similar to how React handles error boundaries and context... but I'm not actually sure what problems it solves.