{"action":"created","issue":{"url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4951","repository_url":"https://api.github.com/repos/jashkenas/coffeescript","labels_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4951/labels{/name}","comments_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4951/comments","events_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4951/events","html_url":"https://github.com/jashkenas/coffeescript/issues/4951","id":298237038,"number":4951,"title":"CS2 Discussion: Features: Block assignment operator","user":{"login":"coffeescriptbot","id":36576729,"avatar_url":"https://avatars1.githubusercontent.com/u/36576729?v=4","gravatar_id":"","url":"https://api.github.com/users/coffeescriptbot","html_url":"https://github.com/coffeescriptbot","followers_url":"https://api.github.com/users/coffeescriptbot/followers","following_url":"https://api.github.com/users/coffeescriptbot/following{/other_user}","gists_url":"https://api.github.com/users/coffeescriptbot/gists{/gist_id}","starred_url":"https://api.github.com/users/coffeescriptbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coffeescriptbot/subscriptions","organizations_url":"https://api.github.com/users/coffeescriptbot/orgs","repos_url":"https://api.github.com/users/coffeescriptbot/repos","events_url":"https://api.github.com/users/coffeescriptbot/events{/privacy}","received_events_url":"https://api.github.com/users/coffeescriptbot/received_events","type":"User","site_admin":false},"labels":[{"id":841194363,"url":"https://api.github.com/repos/jashkenas/coffeescript/labels/new%20feature","name":"new feature","color":"ededed","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":31,"created_at":"2018-02-19T11:08:13Z","updated_at":"2018-02-19T11:09:05Z","closed_at":"2018-02-19T11:08:14Z","author_association":"COLLABORATOR","body":"_From @GeoffreyBooth on [2016-12-05 07:21](https://github.com/coffeescript6/discuss/issues/58)_\n\n\nSplitting off from #35, this proposal is for just one new operator: `:=`, the block assignment operator. So the 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 genuinely new feature added in ES2015, and a great improvement over `var`, which explains why `let` and `const` have become popular features. This block scoping that `let` and `const` each offer is probably something that CoffeeScript should have, separate from the feature of `const` that means “throw an error on reassignment.”\r\n\r\nThe new operator would behave similarly to `=`:\r\n- `a = 1` means, “declare `a` at the top of this function scope using `var`, and assign `a` here with the value of `1`.”\r\n- `a := 1` would mean, “declare `a` at the top of this block scope using `let`, and assign `a` here with the value of `1`.”\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 scope similar to how `var` declarations are currently grouped at the top of their function scope.\r\n\r\nWhat about `const`? Well, there’s really no reason we need it. It protects against reassignment of variables, and that’s all it does. Per [this comment](https://github.com/decaffeinate/decaffeinate/issues/408#issuecomment-264758129), it probably has no performance benefit in runtimes; and since `const` reassignments can be caught by transpilers (whether CoffeeScript or Babel) such reassignments are in practice usually caught at compile time, not run time. If we decide we want to provide a way to output `const`, for full expressibility of ES2015 and for this protection against reassignment, that could be a separate feature added later.\r\n\r\nSo this CoffeeScript:\r\n\r\n``` coffee\r\na = 1\r\nb := 2\r\n\r\nif (yes)\r\n  b := 3\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\n\r\nif (true) {\r\n  let b;\r\n\r\n  b = 3;\r\n}\r\n```\r\n\r\n**Note about bikeshedding:** Please refrain from feedback that says how you prefer the `let` keyword to `:=`, or that you prefer some other sequence of characters to `:=`. Ultimately the keyword or operator chosen is a personal preference decision, and will be made by @jashkenas and whoever implements this. There was plenty of discussion about this on #35. Thanks."},"comment":{"url":"https://api.github.com/repos/jashkenas/coffeescript/issues/comments/366657886","html_url":"https://github.com/jashkenas/coffeescript/issues/4951#issuecomment-366657886","issue_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4951","id":366657886,"user":{"login":"coffeescriptbot","id":36576729,"avatar_url":"https://avatars1.githubusercontent.com/u/36576729?v=4","gravatar_id":"","url":"https://api.github.com/users/coffeescriptbot","html_url":"https://github.com/coffeescriptbot","followers_url":"https://api.github.com/users/coffeescriptbot/followers","following_url":"https://api.github.com/users/coffeescriptbot/following{/other_user}","gists_url":"https://api.github.com/users/coffeescriptbot/gists{/gist_id}","starred_url":"https://api.github.com/users/coffeescriptbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coffeescriptbot/subscriptions","organizations_url":"https://api.github.com/users/coffeescriptbot/orgs","repos_url":"https://api.github.com/users/coffeescriptbot/repos","events_url":"https://api.github.com/users/coffeescriptbot/events{/privacy}","received_events_url":"https://api.github.com/users/coffeescriptbot/received_events","type":"User","site_admin":false},"created_at":"2018-02-19T11:09:03Z","updated_at":"2018-02-19T11:09:03Z","author_association":"COLLABORATOR","body":"_From @GeoffreyBooth on [2016-12-06 18:04](https://github.com/coffeescript6/discuss/issues/58#issuecomment-265224276)_\n\n\n@jashkenas Yes, that’s how a switch to “only block scoping” would work. My point was that I think there’s a middle ground, where we can kind of “automatically” block scope in the cases where people most want block scoping (temporary variables used only within their block scope) without getting rid of automatically function-scoped variables.\r\n\r\nI think getting rid of automatically function-scoped variables is too drastic of a breaking change. Many of the CoffeeScript examples and tutorials across the Web would no longer work."}}
{"actor":{"display_login":"coffeescriptbot"}}