-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@enum implementation #5842
@enum implementation #5842
Conversation
Just realised the second convert() can be pulled out and made generic. Apologies for leaving the Sort example in the code too - will need to fix that :) |
see also #2988 for a more comprehensive implementation of enum functionality. adding this dict-like mapping would be good there too |
Thanks @vtjnash - I did look at #2988 but opted to just attack the pure enum, and leave enum flags for another PR. I also wanted to avoid a few other things like making the enum type iterable etc. The thing I don't like about my PR is the fact that enum members are not scoped. As discussed on julia-users this is waiting for |
Recording my proposal here so people don't have to click through and find it. With overloadable field access, we can just create a getfield(::Type{Direction}, ::Field{:NORTH}) = Direction(0)
getfield(::Type{Direction}, ::Field{:EAST}) = Direction(1)
getfield(::Type{Direction}, ::Field{:WEST}) = Direction(2)
getfield(::Type{Direction}, ::Field{:SOUTH}) = Direction(3) That would make Direction simultaneously a type and let it act like a "namespace" in the sense that you can do Direction.NORTH and get the value Direction(0). The definition of the type would be something like this: immutable Direction <: Enum
value::Cint
end Enum values are thus completely memory compatible with C enums. Not sure if we need a new keyword for this – especially considering that we'll want something very similar for flags – i.e. enums where the values are powers of two. Having |
I like Stefans proposal a lot and hope that it finds consensus. What remains kind of open is the question how the interface (i.e. the |
Is this this |
That is an initial take on it. |
I am suddenly full of dread. If one were to define
things would be incredibly broken. Or we'd have to be careful to use |
It seems like either that (which might be sane, though perhaps not terribly beautiful), or not allow to override But shouldn't we really abstract things like |
I was also a bit surprised – and a bit worried – by how powerful the field overloading feature is. |
I know you don't like underscore-oriented programming, but we could consider field names with some number/pattern of underscores to be reserved for the system. |
Here's an attempt to improve the @enum implementation (still in examples for now). You can do things like:
Issues:
Cint
equivalent used. Due to the complexity I decided to start with simpleCint
first.Feedback welcome :)