Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created January 20, 2020 06:42
Show Gist options
  • Save tail-call/3ea1cdb9fc22648275a8e4f202df400d to your computer and use it in GitHub Desktop.
Save tail-call/3ea1cdb9fc22648275a8e4f202df400d to your computer and use it in GitHub Desktop.
Imagine someone writing a whole app like this
function subtract({ [subtract.minuend]: minuend, [subtract.subtrahend]: subtrahend }) {
return {
[subtract.difference]: minuend - subtrahend
};
}
subtract.minuend = Symbol('subtract.minuend');
subtract.subtrahend = Symbol('subtract.subtrahend');
subtract.difference = Symbol('subtract.difference');
subtract({
[subtract.minuend]: 10,
[subtract.subtrahend]: 7,
})[subtract.difference]; // => 3
// Now imagine there's a huge object passed around with params to every possible function...
@tail-call
Copy link
Author

Actually I love the idea of returning objects for each calls.

Pros:
➕ Every parameter's name is visible in function calls.
➕ Multiple results are trivial (the choice between array/object/closure/global map key is always in favor of object).
➕ Result value is coupled with its intended semantic. If we're using symbols, the coupling can be as unambiguous as it can get.
➕ Go-style error checking¹ for free: just return truthy value in "error" property (or even Symbol.for('error'), App.symbol.error, App.instance.getErrorSymbolForNamespace(...) if you're into that sort of things).
➕ Returned objects can be proxies, which I imagine could be useful for lazy computations and all sorts of smart h4xx to solve business problems in complex domains in a lean and efficient way.

Cons:
➖ Verbose.
➖ Feels wrong.
➖ Possible performance overhead. Though from what I know V8 is good at optimizing that, so this warrants further investigation.
➖ If you want strong typing, you know where to get one.


¹ Yes, I hate JS exceptions. No, I don't just hate exceptions in general: e.g. I enjoy how Swift does them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment