{"action":"created","issue":{"url":"https://api.github.com/repos/manastech/crystal/issues/274","labels_url":"https://api.github.com/repos/manastech/crystal/issues/274/labels{/name}","comments_url":"https://api.github.com/repos/manastech/crystal/issues/274/comments","events_url":"https://api.github.com/repos/manastech/crystal/issues/274/events","html_url":"https://github.com/manastech/crystal/issues/274","id":50458562,"number":274,"title":":= inside if?","user":{"login":"asterite","id":209371,"avatar_url":"https://avatars.githubusercontent.com/u/209371?v=3","gravatar_id":"","url":"https://api.github.com/users/asterite","html_url":"https://github.com/asterite","followers_url":"https://api.github.com/users/asterite/followers","following_url":"https://api.github.com/users/asterite/following{/other_user}","gists_url":"https://api.github.com/users/asterite/gists{/gist_id}","starred_url":"https://api.github.com/users/asterite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asterite/subscriptions","organizations_url":"https://api.github.com/users/asterite/orgs","repos_url":"https://api.github.com/users/asterite/repos","events_url":"https://api.github.com/users/asterite/events{/privacy}","received_events_url":"https://api.github.com/users/asterite/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/manastech/crystal/labels/compiler","name":"compiler","color":"fef2c0"},{"url":"https://api.github.com/repos/manastech/crystal/labels/draft","name":"draft","color":"00B8B8"},{"url":"https://api.github.com/repos/manastech/crystal/labels/RFC","name":"RFC","color":"009800"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":16,"created_at":"2014-11-30T13:56:38Z","updated_at":"2015-09-29T12:14:43Z","closed_at":null,"body":"Since in Crystal it's so common to assign to a variable inside an if to avoid the nil type, this idiom is common:\r\n\r\n```crystal\r\nif var = some_expression\r\n  ...\r\nend\r\n```\r\n\r\nFor example:\r\n\r\n```crystal\r\nif value = hash[\"key\"]?\r\nend\r\n```\r\n\r\nWhen I read it I never mistake the `=` with an `==`, but maybe because I'm so used to writing that kind of code). For others that might not be the case (\"did he mean `==` there?\").\r\n\r\nThe alternatives are:\r\n\r\n1. Allow using `:=` as an alternative to `=`, anywhere.\r\n2. Allow using `:=` as an alternative to `=`, but only inside `if`, `unless`, `while` and `until` conditions.\r\n3. Disallow `=` inside conditions and always require to write `:=` for assignment.\r\n4. Leave things as they are now.\r\n\r\nIn all of these cases `:=` would just be the same as writing `=`. In **no case** it means \"declare this variable and give a compile error if it's already declared\".\r\n\r\nI think I like number 3 the most.\r\n\r\nWhat do others think about this?"},"comment":{"url":"https://api.github.com/repos/manastech/crystal/issues/comments/144041726","html_url":"https://github.com/manastech/crystal/issues/274#issuecomment-144041726","issue_url":"https://api.github.com/repos/manastech/crystal/issues/274","id":144041726,"user":{"login":"alanwillms","id":704204,"avatar_url":"https://avatars.githubusercontent.com/u/704204?v=3","gravatar_id":"","url":"https://api.github.com/users/alanwillms","html_url":"https://github.com/alanwillms","followers_url":"https://api.github.com/users/alanwillms/followers","following_url":"https://api.github.com/users/alanwillms/following{/other_user}","gists_url":"https://api.github.com/users/alanwillms/gists{/gist_id}","starred_url":"https://api.github.com/users/alanwillms/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alanwillms/subscriptions","organizations_url":"https://api.github.com/users/alanwillms/orgs","repos_url":"https://api.github.com/users/alanwillms/repos","events_url":"https://api.github.com/users/alanwillms/events{/privacy}","received_events_url":"https://api.github.com/users/alanwillms/received_events","type":"User","site_admin":false},"created_at":"2015-09-29T12:14:43Z","updated_at":"2015-09-29T12:14:43Z","body":"If you don't like assignments in conditions, just assign before and don't use them. I don't see the point of all the work of creating a new assignment operator and confusing new users.\r\n\r\nOscar's warning idea is good. Some IDEs like Netbeans already do something similar, displaying a warning when you assign a variable in a condition."}}