Hacker News new | ask | show | jobs
by sakuronto 2974 days ago
Interestingly, the match construct is an expression, which I don't think JavaScript has m/any of. Perhaps they could retroactively make if/else expressions, if that doesn't break back-compat.
7 comments

"do" expressions have been proposed as well: https://github.com/tc39/proposal-do-expressions

Babel supports it if you want to live on the bleeding edge: https://babeljs.io/docs/plugins/transform-do-expressions/

It would be kind of neat if JSX implicitly used "do" expressions in children expressions, though the ternary operator is already very useful in JSX, and more concise [1]

1. https://prettier.io/playground/#N4Igxg9gdgLgprEAuc0DOMAEBhCB...

`request.statusCode == 200 && { body: {} } || request.statusCode == 404 && 'Not Found' || throw new Error('Something happened')`

This is basically what they could translate into without having if/else or nested ternaries.

I'm not sure if it is much better than either but I like it more.

That would be really cool. This match feels a lot like Rust's which is good.
I would argue that it doesn't feel like JavaScript much which seems bad.
Why? To me it seems pretty aligned with the "feel" of destructuring.
In my mind there are several valid options out there now:

1. Regex style: new match(input, options);

or built in function match(foo, bar);

or like switch match (foo){ cases

}

But, to have the switch statement syntax and return a value seems like a less than good way to implement this.

I'd argue they've already started going down the "doesn't feel much like JavaScript" road when they started introducing things like fat arrows. I agree that it is bad.
Along with the class syntax (however convenient, JS is not a class-based language).

Aren't decorators also in the works?

Then you would like to have statements as first class objects (e.g., to build them and pass around) and you are one step away from monads of Haskell.

Then you HAVE TO HAVE types, otherwise reasoning about effects in these constructed statements would break your mental spine.

Blasphemy.

The ternary operator ?: is an expression, but for expressions containing statement blocks, you have function() {} and () => {}, including the immediately invoked function expression, (function() {}())
Javascript already has a conditional expression, the ternary operator: `condition ? expr-if-true : expr-if-false`.
It doesn't have code blocks (aside from self-executing lambdas) and is very hard to reason about when nested.
What do you mean by expression? In the common usage, javascript has plenty of them. E.G. "asdf", 1 + 2, [3, 4], {"foo": "bar"}, and fn(arg).

Not to mention the if/else expression, a ? b : c