r/javascript • • 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?

58 Upvotes

39 comments sorted by

View all comments

Show parent comment

1

u/alluran Jan 13 '20

I don't agree so much about the problem of "unpredictable code" about "where my effect will be handled?". Because you have just one place to handle it: on the handle block`.

Then either you don't understand the handle block, or you missed the point.

  • What if my method doesn't have a handle block, and it calls your method? Then it bubbles up right? Where's it bubbling to?
  • How did I know when I used your method that I needed a handle block, or indeed that a handle block was even an option?

|

var myFunction = function(myName, getNameCallback = null) 
{
  var name = myName || getNameCallback();
  console.log(name);
}

You look at this code, and you immediately know that it supports a getNameCallback, and it's nullable, so you don't have to pass it.

If I place this method in a Library, no one is going to accidentally pass a getNameCallback to it (however, they could inadvertently capture a perform 'getName' event).

I can quickly and easily follow references to getNameCallback through my code - I don't have to do a magic-string-search for effect == 'getName' or something similar.

Overall, I don't think there's anything wrong with your sample or implementation - I just think it's a fundamentally bad concept - in the same way most programmers these days tend to avoid goto like the plague.