automatic export of all enum values

435 views
Skip to first unread message

Pieterjan Robbe

unread,
Apr 26, 2016, 11:07:45 AM4/26/16
to julia-users
Is it possible to export all values of an an enum defined inside a module?
That is, without rewriting all values after 'export'.

julia> module Compass

    export WindDirection

    @enum WindDirection north east south west

end


julia> using Compass


julia> WindDirection

Compass.WindDirection


julia> north

ERROR: UndefVarError: north not defined




Steven G. Johnson

unread,
Apr 26, 2016, 12:13:22 PM4/26/16
to julia-users


On Tuesday, April 26, 2016 at 11:07:45 AM UTC-4, Pieterjan Robbe wrote:
Is it possible to export all values of an an enum defined inside a module?
That is, without rewriting all values after 'export'.

for s in instances(WindDirection)

    @eval export $(symbol(s))

end 

Pieterjan Robbe

unread,
Apr 26, 2016, 1:48:07 PM4/26/16
to julia-users
nice solution, thanks!

Op dinsdag 26 april 2016 18:13:22 UTC+2 schreef Steven G. Johnson:

David P. Sanders

unread,
Apr 26, 2016, 1:56:35 PM4/26/16
to julia-users
The instances function (that I didn't know about) doesn't seem to be shown by `methodswith` of the enumeration.
How could we improve this?

julia> @enum FRUIT apple=1 pear=2

julia> methodswith(FRUIT)
3-element Array{Method,1}:
 isless(::FRUIT, ::FRUIT) at Enums.jl:84
 print(::IO, ::FRUIT) at Enums.jl:89
 show(::IO, ::FRUIT) at Enums.jl:95

julia> methodswith(FRUIT, true)
5-element Array{Method,1}:
 convert{T<:Integer}(::Type{T<:Integer}, x::Enum) at Enums.jl:9
 isless(::FRUIT, ::FRUIT) at Enums.jl:84
 print(::IO, ::FRUIT) at Enums.jl:89
 show(::IO, ::FRUIT) at Enums.jl:95
 write(io::IO, x::Enum) at Enums.jl:11

julia> instances(FRUIT)
(apple::FRUIT,pear::FRUIT)

Yichao Yu

unread,
Apr 26, 2016, 2:14:00 PM4/26/16
to Julia Users
On Tue, Apr 26, 2016 at 1:56 PM, David P. Sanders <dpsa...@gmail.com> wrote:
>
>
> El martes, 26 de abril de 2016, 12:13:22 (UTC-4), Steven G. Johnson
> escribió:
>>
>>
>>
>> On Tuesday, April 26, 2016 at 11:07:45 AM UTC-4, Pieterjan Robbe wrote:
>>>
>>> Is it possible to export all values of an an enum defined inside a
>>> module?
>>> That is, without rewriting all values after 'export'.
>>
>>
>> for s in instances(WindDirection)
>>
>> @eval export $(symbol(s))
>>
>> end
>
>
>
> The instances function (that I didn't know about) doesn't seem to be shown
> by `methodswith` of the enumeration.
> How could we improve this?

You need to pass in the type of the argument, not the argument itself,
to methodswith

julia> @enum FRUIT apple=1 pear=2

julia> methodswith(Type{FRUIT})
5-element Array{TypeMapEntry,1}:
convert(::Type{FRUIT}, x::Integer) at Enums.jl:79
instances(::Type{FRUIT}) at Enums.jl:86
typemax(x::Type{FRUIT}) at Enums.jl:83
typemin(x::Type{FRUIT}) at Enums.jl:82
writemime(io::IO, ::MIME{symbol("text/plain")}, ::Type{FRUIT}) at Enums.jl:103

David P. Sanders

unread,
Apr 27, 2016, 12:30:30 AM4/27/16
to julia-users
Thanks. Tricky!
Reply all
Reply to author
Forward
0 new messages