Skip to content
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

Enums are not indexable with integers anymore with Julia 0.7 #28768

Closed
s-celles opened this issue Aug 19, 2018 · 3 comments
Closed

Enums are not indexable with integers anymore with Julia 0.7 #28768

s-celles opened this issue Aug 19, 2018 · 3 comments

Comments

@s-celles
Copy link
Contributor

Hello,

I noticed that enums are not indexable anymore with Julia 0.7.

Julia 0.6

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.4 (2018-07-09 19:09 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-apple-darwin14.5.0

julia> @enum Fruit apple orange kiwi

julia> Fruit
Enum Fruit:
apple = 0
orange = 1
kiwi = 2

julia> Fruit[2]
1-element Array{Fruit,1}:
 kiwi

julia> Fruit[2][1]
kiwi::Fruit = 2

Julia 0.7

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.7.0 (2018-08-08 06:46 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-apple-darwin14.5.0

julia> @enum Fruit apple orange kiwi

julia> Fruit
Enum Fruit:
apple = 0
orange = 1
kiwi = 2

julia> Fruit[2]
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Fruit
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:154
  Fruit(::Integer) at Enums.jl:131
Stacktrace:
 [1] setindex!(::Array{Fruit,1}, ::Int64, ::Int64) at ./array.jl:769
 [2] getindex(::Type{Fruit}, ::Int64) at ./array.jl:366
 [3] top-level scope at none:0

Is there any workaround? because I need to get key from value like it was previously possible.

Kind regards

@yuyichao
Copy link
Contributor

Fruit[2][1]

That was a overly complicated way to spell Fruit(2).

@s-celles
Copy link
Contributor Author

Thanks @yuyichao

s-celles pushed a commit to s-celles/julia that referenced this issue Aug 19, 2018
@s-celles
Copy link
Contributor Author

I have just created a PR to improve docstring of enum #28769

Something else is missing in the doc...
How can we get the underlying type of an enum ?

julia> @enum Fruit apple=1 orange=2 kiwi=3

julia> typeof(Int(apple))
Int64

julia> typeof(Integer(apple))
Int32

This is probably obvious for you... but not for me...
and it could help JuliaIO/LibSerialPort.jl#19

KristofferC pushed a commit that referenced this issue Aug 19, 2018
KristofferC pushed a commit that referenced this issue Aug 20, 2018
Helps #28768

(cherry picked from commit b6b471c)
staticfloat pushed a commit that referenced this issue Aug 24, 2018
KristofferC pushed a commit that referenced this issue Sep 8, 2018
Helps #28768

(cherry picked from commit b6b471c)
KristofferC pushed a commit that referenced this issue Sep 8, 2018
Helps #28768

(cherry picked from commit b6b471c)
KristofferC pushed a commit that referenced this issue Feb 11, 2019
Helps #28768

(cherry picked from commit b6b471c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants