{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/10168","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/10168/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/10168/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/10168/events","html_url":"https://github.com/JuliaLang/julia/pull/10168","id":57395780,"number":10168,"title":"RFC: Add Enums module for enum support through the `@enum` macro","user":{"login":"quinnj","id":2896623,"avatar_url":"https://avatars.githubusercontent.com/u/2896623?v=3","gravatar_id":"","url":"https://api.github.com/users/quinnj","html_url":"https://github.com/quinnj","followers_url":"https://api.github.com/users/quinnj/followers","following_url":"https://api.github.com/users/quinnj/following{/other_user}","gists_url":"https://api.github.com/users/quinnj/gists{/gist_id}","starred_url":"https://api.github.com/users/quinnj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/quinnj/subscriptions","organizations_url":"https://api.github.com/users/quinnj/orgs","repos_url":"https://api.github.com/users/quinnj/repos","events_url":"https://api.github.com/users/quinnj/events{/privacy}","received_events_url":"https://api.github.com/users/quinnj/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":17,"created_at":"2015-02-11T23:44:20Z","updated_at":"2015-02-21T22:33:19Z","closed_at":"2015-02-21T16:41:59Z","pull_request":{"url":"https://api.github.com/repos/JuliaLang/julia/pulls/10168","html_url":"https://github.com/JuliaLang/julia/pull/10168","diff_url":"https://github.com/JuliaLang/julia/pull/10168.diff","patch_url":"https://github.com/JuliaLang/julia/pull/10168.patch"},"body":"This builds largely on and supersedes the work by @vtjnash in #2988 (minus the bitmask business), with some modifications in line with comments in #3080 and the [python PEP for enums](https://www.python.org/dev/peps/pep-0435/#proposed-semantics-for-the-new-enumeration-type).\r\n\r\nBasically the usage is:\r\n```julia\r\nIn  [3]: @enum Fruit apple orange kiwi\r\n\r\nIn  [4]: Fruit <: Base.Enums.Enum\r\n\r\nOut [4]: true\r\n\r\nIn  [5]: apple\r\n\r\nOut [5]: apple::Fruit\r\n\r\nIn  [6]: orange\r\n\r\nOut [6]: orange::Fruit\r\n\r\nIn  [7]: Fruit(0)\r\n\r\nOut [7]: apple::Fruit\r\n\r\nIn  [8]: Fruit(2)\r\n\r\nOut [8]: kiwi::Fruit\r\n\r\nIn  [9]: f(x::Fruit) = \"hey, I'm a Fruit\"\r\n\r\nOut [9]: f (generic function with 1 method)\r\n\r\nIn  [10]: f(apple) == \"hey, I'm a Fruit\"\r\n\r\nOut [10]: true\r\n\r\nIn  [11]: f(Fruit(3))\r\n\r\nOut [11]: ERROR: LoadError: invalid enum value for Fruit: 3\r\nwhile loading In[11], in expression starting on line 1\r\n\r\n in error at error.jl:19\r\n in call at Enums.jl:22\r\n in call at Enums.jl:20\r\n```\r\n\r\nThe RFC status is because there are still a few design questions that may be worth discussing:\r\n\r\n* Currently negative enum values are allowed (e.g. `@enum Fruit apple=-1`); I couldn't really think of a reason to disallow them; I think in some languages they're disallowed however\r\n* There's no overflow checking when assigning default enum member values (e.g. `@enum Fruit apple=0xff orange`, `orange` will have value `0x00`); I don't think this is a big deal in practice, but while writing tests it came up\r\n* @JeffBezanson made a comment [here](https://github.com/JuliaLang/julia/issues/3080#issuecomment-17767565) about being able to add another subtype under `Enums.Enum` (could be used in a module for groups of enums, for example); we could allow this through syntax like `@enum YellowFruits=Fruit banana lemon`, but I'm not sure if that's pretty or useful enough\r\n* Scoping: in python, you create an enum by creating a class that subtypes `enum` and enum members are fields of that class; this provides a nice scoping for enum members that *belong* to the enum and don't leak out into the current module namespace. In this implementation, enum member values are just `const enum_value = EnumType(x)`, which isn't bad, except if you inadvertently do something like `@enum MyImportantEnum zero=0 one=1`, which overwrites the bindings for the Base functions `zero` and `one`. One idea I had for this would be, in the `@enum` macro call, to wrap the enum type definition and enum member assignments in a baremodule. With #1974, you could then access enum member values only by doing something like `Fruit.apple` (which is python's syntax) and this would access the enum member value within the auto-generated baremodule.\r\n\r\nI'd appreciate any comments/feedback."},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/75396781","html_url":"https://github.com/JuliaLang/julia/pull/10168#issuecomment-75396781","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/10168","id":75396781,"user":{"login":"nalimilan","id":1120448,"avatar_url":"https://avatars.githubusercontent.com/u/1120448?v=3","gravatar_id":"","url":"https://api.github.com/users/nalimilan","html_url":"https://github.com/nalimilan","followers_url":"https://api.github.com/users/nalimilan/followers","following_url":"https://api.github.com/users/nalimilan/following{/other_user}","gists_url":"https://api.github.com/users/nalimilan/gists{/gist_id}","starred_url":"https://api.github.com/users/nalimilan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nalimilan/subscriptions","organizations_url":"https://api.github.com/users/nalimilan/orgs","repos_url":"https://api.github.com/users/nalimilan/repos","events_url":"https://api.github.com/users/nalimilan/events{/privacy}","received_events_url":"https://api.github.com/users/nalimilan/received_events","type":"User","site_admin":false},"created_at":"2015-02-21T22:33:19Z","updated_at":"2015-02-21T22:33:19Z","body":"+1 for renaming `names` to `fieldnames` or `fields`. In addition to DataFrames, we need `names` in the context of NamedArrays (arrays with row/column/dimension names), and I can't find different terminology that would be natural."}}