-
Notifications
You must be signed in to change notification settings - Fork 57
Self-documentation: refactor enums? #11
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
Comments
What do you think of this? module Cairo
format = @ CEnum [
-1, :invalid,
:argb32,
:rgb24,
...
]
@ assert format.argb32 == 0 # true
end
Note that negative and non-incremental indices must come before their names. abstract CEnum
module Base
function show{T<:CEnum}(io::IO, enum::T)
for name in sort(map(string,[names(enum)...]))
print(io, "$name\n")
end
end
macro CEnum(symbols)
symbols = eval(symbols)
typ = symbol(string("CEnum",gensym()))
names = Any[] # `Any` is required for the `Expr(...)` below
values = Int[]
i = isa(symbols[1], Integer) ? symbols[1] : 0
first = true
for e in symbols
if isa (e, Symbol)
push!(names, e)
push!(values, i)
i = i +1
elseif isa(e, Integer)
if e <= i && !first
error("Bad index in enum: $e")
end
i = e
else
error("Bad type in enum: $e is a $(typeof(e)). Int or Symbol expected.")
end
first = false
end
Expr(:block, Any[
Expr(:type, Any[
Expr(:<:, Any[typ,:CEnum], Any),
Expr(:block, names, Any)
], Any),
Expr(:call, Any[typ, values...], Any)
], Any)
end
end #Base |
I'm actually not too opposed to keeping the C enums in this case since this is a rather low level library and people using it will be looking at the Cairo docs, which list the C enums. Having to translate seems like an unnecessary obstacle. |
The translation is trivial ( It enables the possiblity to cache BTW, this is a suggestion for a general convention for representing enums in Julia. We don't have to follow the conventions of other languages when we can do better. |
Enums are still under discussion in base Julia, you may want to consider bringing this up there. |
I agree w/ @loladiro -- let's keep the C names. |
Pipe dream: it would be nice to have
It could be implemented as descendants of an abstract CConst/CEnum type (for the
show()
implementation), built with a macro that takes a name, an offset and an array of symbols (withnothing
for holes) as parameters. A variation could take Dict with symbols as keys.Once immutable types are implemented, this should be as efficient as traditional
const
values.The text was updated successfully, but these errors were encountered: