5 ms·
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
by sakuronto 8y 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.
- cevn 8y agoThat would be really cool. This match feels a lot like Rust's which is good.
- c0nfused 8y agoI would argue that it doesn't feel like JavaScript much which seems bad.
- irrational 8y agoI'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.
- goatlover 8y agoAlong with the class syntax (however convenient, JS is not a class-based language). Aren't decorators also in the works?
- kristiandupont 8y agoWhy? To me it seems pretty aligned with the "feel" of destructuring.
- c0nfused 8y agoIn 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.
- dtech 8y agoJavascript already has a conditional expression, the ternary operator: `condition ? expr-if-true : expr-if-false`.
- hajile 8y agoIt doesn't have code blocks (aside from self-executing lambdas) and is very hard to reason about when nested.
- madamelic 8y ago`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.
- CGamesPlay 8y agoThe ternary operator ?: is an expression, but for expressions containing statement blocks, you have function() {} and () => {}, including the immediately invoked function expression, (function() {}())
- tlrobinson 8y ago"do" expressions have been proposed as well: https://github.com/tc39/proposal-do-expressions 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/ 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/#N4Igxg9gdgLgprEAuc0DOMAEBhCBbAB2gRgHUBLGACwBEJMBeTACmEwDMJ6BfASkYB8mADwATcgDcBwUfTbl2zThH5thaAgEMoAquWEB6DdqHdMcADZo4mNeKkAjAJ6H7p7t1eSBAHSh-IKAwcfCIoEgpqABU4ACcoTVinRhY2ZUw+QRE3NK5MAH4RYx09Q2KhJGzvZy8pDNqBEAAaEAgCGHJ0ZFBE2IgAdwAFRIQ0ZBBNCQhyUWaQB1jNMABrOBgAZS0wcigAc2QYWIBXOBad61iYQcXdvE1kdk0rU5AAKzQADwAhRZW19c0eDgABkdnAHk9rC0tLELuMHJpnBZoHMCLEdmQZtRkAAOAAM0L61lIiwI4zRcAuEnBLVicAAjkdyHTrppbvckI9ni1rHhyAdji80DtdhY4ABFI4QeAQ7kgGCIiiibFIABMLUOmnIFhFuDwd3GUGIcyO1iiiLGnMhcA8QA https://prettier.io/playground/#N4Igxg9gdgLgprEAuc0DOMAEBhCB...
- recursive 8y agoWhat 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
- thesz 8y agoThen 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.