{"action":"created","issue":{"url":"https://api.github.com/repos/coffeescript6/discuss/issues/35","repository_url":"https://api.github.com/repos/coffeescript6/discuss","labels_url":"https://api.github.com/repos/coffeescript6/discuss/issues/35/labels{/name}","comments_url":"https://api.github.com/repos/coffeescript6/discuss/issues/35/comments","events_url":"https://api.github.com/repos/coffeescript6/discuss/issues/35/events","html_url":"https://github.com/coffeescript6/discuss/issues/35","id":176304837,"number":35,"title":"Block assignment let/const operator","user":{"login":"GeoffreyBooth","id":456802,"avatar_url":"https://avatars.githubusercontent.com/u/456802?v=3","gravatar_id":"","url":"https://api.github.com/users/GeoffreyBooth","html_url":"https://github.com/GeoffreyBooth","followers_url":"https://api.github.com/users/GeoffreyBooth/followers","following_url":"https://api.github.com/users/GeoffreyBooth/following{/other_user}","gists_url":"https://api.github.com/users/GeoffreyBooth/gists{/gist_id}","starred_url":"https://api.github.com/users/GeoffreyBooth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GeoffreyBooth/subscriptions","organizations_url":"https://api.github.com/users/GeoffreyBooth/orgs","repos_url":"https://api.github.com/users/GeoffreyBooth/repos","events_url":"https://api.github.com/users/GeoffreyBooth/events{/privacy}","received_events_url":"https://api.github.com/users/GeoffreyBooth/received_events","type":"User","site_admin":false},"labels":[{"id":445097887,"url":"https://api.github.com/repos/coffeescript6/discuss/labels/new%20feature","name":"new feature","color":"647A59","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":30,"created_at":"2016-09-12T06:46:15Z","updated_at":"2016-12-05T01:53:43Z","closed_at":null,"body":"Building off of #1 and [this comment](https://github.com/coffeescript6/discuss/issues/1#issuecomment-245963553), if I can paraphrase @zeekay (though @zeekay if you’d like to write a proposal for this please post it and I’ll put it here):\r\n\r\nThe great advantage of `let`/`const` is its block-scoping, i.e.:\r\n\r\n``` js\r\nlet a = 1;\r\nif (true) {\r\n  let b = 2;\r\n}\r\nconsole.log(b); // undefined\r\n```\r\n\r\nThis is a dramatic improvement over `var`, and a big reason why `let` and `const` have become popular features. We need to at least discuss if this is something CoffeeScript should have, separate from the feature of `const` that means “throw an error on reassignment.”\r\n\r\nI think we could have both, via `:=` and perhaps `:==` operators (or whatever two operators people think are best):\r\n- `a := 1` would mean, “declare and assign `a` with the value of 1, using `let` if `a` gets reassigned in this block and `const` if it never gets reassigned.”\r\n- `a :== 1` would mean, “declare and assign `a` with the value of 1 using `const`. If it gets reassigned later, throw an error.”\r\n\r\nWe don’t necessarily need the second operator, if we don’t care to give people a way to force `const`. I’m not sure how popular the “throw an error on reassignment” feature is, but I suspect it’s quite popular.\r\n\r\n`let` has the same issue as `var`, in that its declaration is hoisted to its entire scope (what [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) refers to as the [temporal dead zone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let)), so `let` declarations should be grouped together at the top of their block similar to how `var` declarations are grouped at the top of their scope. `const` must be declared and assigned on the same line, so it can’t get moved up.\r\n\r\nSo this CoffeeScript:\r\n\r\n``` coffee\r\na = 1\r\nb := 2\r\nc :== 3\r\n\r\nif (yes)\r\n  b := 4\r\n  c :== 5\r\n```\r\n\r\nwould compile into this JavaScript:\r\n\r\n``` js\r\nvar a;\r\nlet b;\r\n\r\na = 1;\r\nb = 2;\r\nconst c = 3;\r\n\r\nif (true) {\r\n  let b;\r\n\r\n  b = 4;\r\n  const c = 5;\r\n}\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/coffeescript6/discuss/issues/comments/264751029","html_url":"https://github.com/coffeescript6/discuss/issues/35#issuecomment-264751029","issue_url":"https://api.github.com/repos/coffeescript6/discuss/issues/35","id":264751029,"user":{"login":"GeoffreyBooth","id":456802,"avatar_url":"https://avatars.githubusercontent.com/u/456802?v=3","gravatar_id":"","url":"https://api.github.com/users/GeoffreyBooth","html_url":"https://github.com/GeoffreyBooth","followers_url":"https://api.github.com/users/GeoffreyBooth/followers","following_url":"https://api.github.com/users/GeoffreyBooth/following{/other_user}","gists_url":"https://api.github.com/users/GeoffreyBooth/gists{/gist_id}","starred_url":"https://api.github.com/users/GeoffreyBooth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GeoffreyBooth/subscriptions","organizations_url":"https://api.github.com/users/GeoffreyBooth/orgs","repos_url":"https://api.github.com/users/GeoffreyBooth/repos","events_url":"https://api.github.com/users/GeoffreyBooth/events{/privacy}","received_events_url":"https://api.github.com/users/GeoffreyBooth/received_events","type":"User","site_admin":false},"created_at":"2016-12-05T01:53:43Z","updated_at":"2016-12-05T01:53:43Z","body":"@just-nobody MoonScript compiles into Lua, not JavaScript. What JavaScript would your example compile to? I’m not aware of a way to introduce a new scope in JavaScript that doesn’t inherit the variables from its parent scope. (Though if you can prove me wrong about this, please start a new issue. I’d like to keep this issue focused on the proposal in the first comment. Thanks!)"}}
{"actor":{"display_login":"GeoffreyBooth"}}