{"action":"created","issue":{"url":"https://api.github.com/repos/leafo/moonscript/issues/77","repository_url":"https://api.github.com/repos/leafo/moonscript","labels_url":"https://api.github.com/repos/leafo/moonscript/issues/77/labels{/name}","comments_url":"https://api.github.com/repos/leafo/moonscript/issues/77/comments","events_url":"https://api.github.com/repos/leafo/moonscript/issues/77/events","html_url":"https://github.com/leafo/moonscript/issues/77","id":10684348,"number":77,"title":"Allow constructors to return a new self","user":{"login":"stevedonovan","id":218893,"avatar_url":"https://avatars.githubusercontent.com/u/218893?v=3","gravatar_id":"","url":"https://api.github.com/users/stevedonovan","html_url":"https://github.com/stevedonovan","followers_url":"https://api.github.com/users/stevedonovan/followers","following_url":"https://api.github.com/users/stevedonovan/following{/other_user}","gists_url":"https://api.github.com/users/stevedonovan/gists{/gist_id}","starred_url":"https://api.github.com/users/stevedonovan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stevedonovan/subscriptions","organizations_url":"https://api.github.com/users/stevedonovan/orgs","repos_url":"https://api.github.com/users/stevedonovan/repos","events_url":"https://api.github.com/users/stevedonovan/events{/privacy}","received_events_url":"https://api.github.com/users/stevedonovan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2013-02-06T08:57:08Z","updated_at":"2016-10-14T06:27:35Z","closed_at":null,"body":"The case I'm thinking of is a List class which acts as a _wrapper_ for a plain table. In particular, it provides methods but remains table-compatible.  (This is how the List class in Penlight is organized).  \r\n\r\nSo the start might look like this:\r\n\r\n```lua\r\nclass List\r\n    new: (t) =>\r\n        if t then return t\r\n```\r\n\r\nThat is, if we say List {1,2,3} we want to wrap that table as our List.\r\n\r\nThis is easily done. In the generated code we have:\r\n\r\n```lua\r\n    __call = function(cls, ...)\r\n      local _self_0 = setmetatable({}, _base_0)\r\n      local _new = cls.__init(_self_0, ...)\r\n      if _new then\r\n          _self_0 = setmetatable(_new,_base_0)\r\n      end\r\n      return _self_0\r\n    end\r\n```\r\nIf any return value of `cls.__init` is non-nil, then it's assumed to be the new self, which is given the right metatable.\r\n\r\nA too-rigid interpretation of OOP would regard this as fishy - allowing a constructor to make a self - making an object which is also an array, etc.  But the beauty of Lua is that we may do exactly what we need to do, and such a little change would give the MS class system some extra flexibility.\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/leafo/moonscript/issues/comments/253719982","html_url":"https://github.com/leafo/moonscript/issues/77#issuecomment-253719982","issue_url":"https://api.github.com/repos/leafo/moonscript/issues/77","id":253719982,"user":{"login":"rosshadden","id":669012,"avatar_url":"https://avatars.githubusercontent.com/u/669012?v=3","gravatar_id":"","url":"https://api.github.com/users/rosshadden","html_url":"https://github.com/rosshadden","followers_url":"https://api.github.com/users/rosshadden/followers","following_url":"https://api.github.com/users/rosshadden/following{/other_user}","gists_url":"https://api.github.com/users/rosshadden/gists{/gist_id}","starred_url":"https://api.github.com/users/rosshadden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rosshadden/subscriptions","organizations_url":"https://api.github.com/users/rosshadden/orgs","repos_url":"https://api.github.com/users/rosshadden/repos","events_url":"https://api.github.com/users/rosshadden/events{/privacy}","received_events_url":"https://api.github.com/users/rosshadden/received_events","type":"User","site_admin":false},"created_at":"2016-10-14T06:27:34Z","updated_at":"2016-10-14T06:27:34Z","body":"The reason I see thrown around is that it could have weird side-effects when returning \"by accident\". Is it not better to further the language than to worry about people making mistakes? They can make mistakes in any part of moonscript. I would personally rather come up with an elegant way to throw an error in such cases, like testing that the metatable of the returned value is the same as the class'. Does that solution sound reasonable?\r\n\r\nI apologize if that sounded condescending or something. I have just witnessed a great deal of unrealized potential in projects in the past for things like this, and it drives me crazy. It's like when one kid does something stupid on the playground, and they take away recess for the whole school. 😲😲😲"}}
{"actor":{"display_login":"rosshadden"}}