{"action":"created","issue":{"url":"https://api.github.com/repos/Hammerspoon/hammerspoon/issues/486","repository_url":"https://api.github.com/repos/Hammerspoon/hammerspoon","labels_url":"https://api.github.com/repos/Hammerspoon/hammerspoon/issues/486/labels{/name}","comments_url":"https://api.github.com/repos/Hammerspoon/hammerspoon/issues/486/comments","events_url":"https://api.github.com/repos/Hammerspoon/hammerspoon/issues/486/events","html_url":"https://github.com/Hammerspoon/hammerspoon/issues/486","id":100405656,"number":486,"title":"code suggestion -- protect tables of constants in modules","user":{"login":"asmagill","id":8139480,"avatar_url":"https://avatars3.githubusercontent.com/u/8139480?v=3","gravatar_id":"","url":"https://api.github.com/users/asmagill","html_url":"https://github.com/asmagill","followers_url":"https://api.github.com/users/asmagill/followers","following_url":"https://api.github.com/users/asmagill/following{/other_user}","gists_url":"https://api.github.com/users/asmagill/gists{/gist_id}","starred_url":"https://api.github.com/users/asmagill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asmagill/subscriptions","organizations_url":"https://api.github.com/users/asmagill/orgs","repos_url":"https://api.github.com/users/asmagill/repos","events_url":"https://api.github.com/users/asmagill/events{/privacy}","received_events_url":"https://api.github.com/users/asmagill/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":14,"created_at":"2015-08-11T20:45:56Z","updated_at":"2017-03-28T07:34:25Z","closed_at":null,"body":"I've put up a gist at https://gist.github.com/asmagill/4340576881a774cac721 with a suggestion for putting metamethods in place to protect tables of constants that we provide in some of our modules.\n\nAnd yes, this suggestion comes because I spent over an hour trying to debug something only to find out I had accidentally used \"=\" instead of \"==\" and changed a supposed \"constant\" and couldn't figure out why my code no longer worked... well, it was a little more complex than that, but... in essence, yeah, I changed a constant and then couldn't figure out why it no longer referred to what I wanted it to.\n\nThe suggestion includes metamethods that prevent setting, unsetting, and changing values in the table once it the hs._makeConstantsTable (a suggestion only, as this would make it available to every module automatically) function is called.  You can also query by key or value (the reverse look found in some of the tables -- e.g. if t = { a = 1, b = 2}, then t.b would return 2 and t[2] would return \"b\")\n\nIt also provides __pairs, so using pairs() on it works, and __tostring, so you can get a quick reference to its keys in the console. In lua 5.3, ipairs now respects metamethods, so that works like you'd expect -- if the values or keys are numeric, it will start at 1 and progress to the first missing index.\n\nIt's also protected from changing the metatable later, so it in effect is about close to truly constant as I think I can make it. (Rawset and rawget could still change/bypass things, but that's pretty explicit and not done by accident.  I'm ok with that.)\n\nThoughts?  I'll certainly be adding such to things I do in the future, but it would require less repetition of code if we made it available to everything that wants to use it.\n"},"comment":{"url":"https://api.github.com/repos/Hammerspoon/hammerspoon/issues/comments/289687872","html_url":"https://github.com/Hammerspoon/hammerspoon/issues/486#issuecomment-289687872","issue_url":"https://api.github.com/repos/Hammerspoon/hammerspoon/issues/486","id":289687872,"user":{"login":"cmsj","id":353427,"avatar_url":"https://avatars3.githubusercontent.com/u/353427?v=3","gravatar_id":"","url":"https://api.github.com/users/cmsj","html_url":"https://github.com/cmsj","followers_url":"https://api.github.com/users/cmsj/followers","following_url":"https://api.github.com/users/cmsj/following{/other_user}","gists_url":"https://api.github.com/users/cmsj/gists{/gist_id}","starred_url":"https://api.github.com/users/cmsj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmsj/subscriptions","organizations_url":"https://api.github.com/users/cmsj/orgs","repos_url":"https://api.github.com/users/cmsj/repos","events_url":"https://api.github.com/users/cmsj/events{/privacy}","received_events_url":"https://api.github.com/users/cmsj/received_events","type":"User","site_admin":false},"created_at":"2017-03-28T07:34:25Z","updated_at":"2017-03-28T07:34:25Z","body":"@asmagill yeah I think it's a good idea :)"}}
{"actor":{"display_login":"cmsj"}}