{"action":"created","issue":{"url":"https://api.github.com/repos/privat/nit/issues/1243","labels_url":"https://api.github.com/repos/privat/nit/issues/1243/labels{/name}","comments_url":"https://api.github.com/repos/privat/nit/issues/1243/comments","events_url":"https://api.github.com/repos/privat/nit/issues/1243/events","html_url":"https://github.com/privat/nit/pull/1243","id":66095972,"number":1243,"title":"new `with` statement","user":{"login":"privat","id":135828,"avatar_url":"https://avatars.githubusercontent.com/u/135828?v=3","gravatar_id":"","url":"https://api.github.com/users/privat","html_url":"https://github.com/privat","followers_url":"https://api.github.com/users/privat/followers","following_url":"https://api.github.com/users/privat/following{/other_user}","gists_url":"https://api.github.com/users/privat/gists{/gist_id}","starred_url":"https://api.github.com/users/privat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/privat/subscriptions","organizations_url":"https://api.github.com/users/privat/orgs","repos_url":"https://api.github.com/users/privat/repos","events_url":"https://api.github.com/users/privat/events{/privacy}","received_events_url":"https://api.github.com/users/privat/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/privat/nit/labels/need_review","name":"need_review","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":13,"created_at":"2015-04-03T07:20:06Z","updated_at":"2015-04-06T17:31:19Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/privat/nit/pulls/1243","html_url":"https://github.com/privat/nit/pull/1243","diff_url":"https://github.com/privat/nit/pull/1243.diff","patch_url":"https://github.com/privat/nit/pull/1243.patch"},"body":"Introduce the `with` statement that is analogous to the `with` of Python or the `uning` of C#.\r\n\r\n~~~nit\r\nwith x = something do\r\n   x.work\r\nend\r\n~~~\r\n\r\nThat is equivalent with\r\n\r\n~~~nit\r\ndo\r\n   var x = something\r\n   x.start\r\n   x.work\r\n   x.finish\r\nend\r\n~~~\r\n\r\nNote that an alternative syntax, without the local variable exists:\r\n\r\n~~~nit\r\nwith something do\r\n   work\r\nend\r\n~~~\r\n\r\nthat is used when we want some automatic control or whatever. Eg resource allocation or critical section.\r\n\r\n~~~nit\r\ndo\r\n   var tmp = something\r\n   tmp.start\r\n   work\r\n   tmp.stop\r\nend\r\n~~~\r\n\r\nMost real-world examples could be\r\n\r\n~~~nit\r\nwith file = path.create do\r\n   file.write(\"Bla\")\r\nend\r\n~~~\r\n\r\nand\r\n\r\n~~~nit\r\nwith a_mutex do\r\n   work\r\nend\r\n~~~\r\n\r\n## Names\r\n\r\nI reused the `finish` method from Iterator.\r\nI introduced the antonym `start` for the other side.\r\n\r\nNote that the `start` is important because it helps the object to know it has to prepare itself for a `finish`.\r\n\r\nI did not introduce an interface, mainly because I have no idea for a name. C# names it `IDisposable` and the `finish` method is called `Dispose`. There is no `start` because C# designers are lunatic sometime.\r\n\r\nIn python there is no interface (obviously) and the method are called \r\n`__enter__` and `__exit__` because Python likes identifiers that resemble words eaten by boas.\r\nNote that the returned variable is not the original expression but the result of `__enter__`.\r\nWith the Python semantic, but the Nit syntax, the first example will be equivalent to\r\n\r\n~~~nit\r\ndo\r\n   var tmp = something\r\n   var x = tmp.start\r\n   x.work\r\n   x.finish\r\nend\r\n~~~\r\n\r\nI am not sure of the benefits of the additional complexity, but the the more I thing about it the more I think this could be useful, ex for the control case the `start` can return the handler to `finish`.\r\n\r\nThe issue is that proposed syntax: `with x = something` does not make sense (because `tmp=something` and `x=tmp.start`) thus should be replaced with something else; Python uses `with something as x:` that we can reuse but I do not want because it is ugly.\r\n\r\nAn other alternative I tough of was to reuse the `for` keyword. but It is convoluted.\r\nThe two previous real world examples will then be\r\n\r\n~~~nit\r\nfor file = path.create do\r\n   file.write(\"Bla\")\r\nend\r\n~~~\r\n\r\nand\r\n\r\n~~~nit\r\nfor a_mutex do\r\n   work\r\nend\r\n~~~"},"comment":{"url":"https://api.github.com/repos/privat/nit/issues/comments/90164414","html_url":"https://github.com/privat/nit/pull/1243#issuecomment-90164414","issue_url":"https://api.github.com/repos/privat/nit/issues/1243","id":90164414,"user":{"login":"egagnon","id":135806,"avatar_url":"https://avatars.githubusercontent.com/u/135806?v=3","gravatar_id":"","url":"https://api.github.com/users/egagnon","html_url":"https://github.com/egagnon","followers_url":"https://api.github.com/users/egagnon/followers","following_url":"https://api.github.com/users/egagnon/following{/other_user}","gists_url":"https://api.github.com/users/egagnon/gists{/gist_id}","starred_url":"https://api.github.com/users/egagnon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/egagnon/subscriptions","organizations_url":"https://api.github.com/users/egagnon/orgs","repos_url":"https://api.github.com/users/egagnon/repos","events_url":"https://api.github.com/users/egagnon/events{/privacy}","received_events_url":"https://api.github.com/users/egagnon/received_events","type":"User","site_admin":false},"created_at":"2015-04-06T17:31:19Z","updated_at":"2015-04-06T17:31:19Z","body":"I have nothing against an automatic variable declaration; I have all against the proposed syntax which is similar to *assignment as expression*, as know in most programming languages.\r\n\r\nHere's an (ulgy?) alternative:\r\n```\r\nwith f in path.create do\r\n  ...\r\nend\r\n```\r\nBut, this also means that we need two syntactic constructs: one with variable declaration, one without."}}