{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/10841","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/10841/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/10841/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/10841/events","html_url":"https://github.com/Microsoft/TypeScript/issues/10841","id":176192966,"number":10841,"title":"`if-else` and `switch` as expressions","user":{"login":"shelby3","id":247731,"avatar_url":"https://avatars.githubusercontent.com/u/247731?v=3","gravatar_id":"","url":"https://api.github.com/users/shelby3","html_url":"https://github.com/shelby3","followers_url":"https://api.github.com/users/shelby3/followers","following_url":"https://api.github.com/users/shelby3/following{/other_user}","gists_url":"https://api.github.com/users/shelby3/gists{/gist_id}","starred_url":"https://api.github.com/users/shelby3/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shelby3/subscriptions","organizations_url":"https://api.github.com/users/shelby3/orgs","repos_url":"https://api.github.com/users/shelby3/repos","events_url":"https://api.github.com/users/shelby3/events{/privacy}","received_events_url":"https://api.github.com/users/shelby3/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Out%20of%20Scope","name":"Out of Scope","color":"556677"},{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Suggestion","name":"Suggestion","color":"006b75"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":15,"created_at":"2016-09-10T18:04:21Z","updated_at":"2016-09-11T14:33:53Z","closed_at":null,"body":"There are scenarios that require we produce a result from an `if-else` or `switch` which is not returned:\r\n\r\n```ts\r\nfunction f(z) {}\r\nconst x = 1, y = 2, z   // Error z must be initialized\r\nif (x) z = y            // Error can't reassign const z\r\nelse z = true\r\nf(z)\r\n```\r\n\r\nEven if we abandon `const z` for `let z`, the code above is noisy (not DNRY, SPOT, and encapsulated) and if we modify the spelling of the identifier `z`, then we have to remember to change it every where it is assigned in the `if-else` (or `switch`)  statement.\r\n\r\nSome languages (e.g. Scala) allow `if-else` and `switch` to optionally be used as expressions:\r\n\r\n```ts\r\nfunction f(z) {}\r\nconst x = 1, y = 2,\r\n      z = if (x) y else true\r\nf(z)\r\n```\r\n\r\nObviously we could use ternary operator expression instead:\r\n\r\n```ts\r\nfunction f(z) {}\r\nconst x = 1, y = 2,\r\n      z = x ? y : true\r\nf(z)\r\n```\r\n\r\nBut that won't help us for `switch`, nor is the ternary able to employ blocks containing (multiple) statements where only the value of the last statement or expression is the result value:\r\n\r\n```ts\r\nfunction f(z) {}\r\nconst x = 1, y = 2,\r\n      z = if (x) { for(var i = 0; ++i < y;); i } else true\r\nf(z)\r\n```\r\n\r\nAdditionally an expression can be placed inline as a function parameter argument:\r\n\r\n\r\n```ts\r\nfunction f(z) {}\r\nconst x = 1, y = 2\r\nf(if (x) { for(var i = 0; ++i < y;); i } else true)\r\n```\r\n\r\nThe transpiling to JavaScript is straightforward:\r\n\r\n```js\r\nfunction f(z) {}\r\nvar x = 1, y = 2\r\nf((function() { if (x) { for(var i = 0; ++i < y;); return i } else return true })())\r\n```\r\n\r\nI presume it is not necessary for me to write multiple line `switch` examples for the reader to visualize the utility of this suggested feature.\r\n\r\nThe only potential blocking issue I am contemplating is whether the parser grammar has any conflict with accepting `if-else` and `switch` as expressions.\r\n\r\nAs example of a language which supports `if-else` and `match` (switch variant) as expressions is Scala. "},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/246183323","html_url":"https://github.com/Microsoft/TypeScript/issues/10841#issuecomment-246183323","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/10841","id":246183323,"user":{"login":"kitsonk","id":1282577,"avatar_url":"https://avatars.githubusercontent.com/u/1282577?v=3","gravatar_id":"","url":"https://api.github.com/users/kitsonk","html_url":"https://github.com/kitsonk","followers_url":"https://api.github.com/users/kitsonk/followers","following_url":"https://api.github.com/users/kitsonk/following{/other_user}","gists_url":"https://api.github.com/users/kitsonk/gists{/gist_id}","starred_url":"https://api.github.com/users/kitsonk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kitsonk/subscriptions","organizations_url":"https://api.github.com/users/kitsonk/orgs","repos_url":"https://api.github.com/users/kitsonk/repos","events_url":"https://api.github.com/users/kitsonk/events{/privacy}","received_events_url":"https://api.github.com/users/kitsonk/received_events","type":"User","site_admin":false},"created_at":"2016-09-11T14:33:53Z","updated_at":"2016-09-11T14:33:53Z","body":">  I had no clue at all that ES-DISCUSS even exists before @kitsonk mentioned it. I am not even clear as to where the official discussion takes place, how to add proposals, etc..\r\n\r\nTechnical Committee 39 is the governing body for ECMAScript (ECMA262).  The current official specification is [here](https://tc39.github.io/ecma262/).  Their GitHub repo is [here](https://github.com/tc39).  There is a [proposals](https://github.com/tc39/proposals) repo and a [process document](https://tc39.github.io/process-document/) of how proposals are progressed.  TC39 meet bimonthly in person to continue to move forward with the standard.  They have [published contribution guidelines](https://github.com/tc39/proposals/blob/master/CONTRIBUTING.md).  For a standards organisation, they are one of the most open and transparent ones, in my opinion.\r\n\r\nMy opinion is that around the time of the ratification of ES2015, the processes bedded down for proposals (because ES2015 was a bit of a ruby scrum) and the process has added a great deal of transparency to the process.  The problem was that ES2016 was fairly light (especially compared to the behemoth that was ES2015) though it is looking like ES2017 is picking up a bit with [5 proposals at Stage 4, ready for ratification](https://github.com/tc39/proposals/blob/master/finished-proposals.md)."}}
{"actor":{"display_login":"kitsonk"}}