# Check if a value is in an enum

**URL:** https://discourse.julialang.org/t/check-if-a-value-is-in-an-enum/31815
**Category:** General Usage
**Tags:** enum
**Created:** [December 3, 2019, 7:12pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-in-an-enum/31815 "2019-12-03T19:12:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)
#### Post date: [December 3, 2019, 7:12pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-in-an-enum/31815/1 "2019-12-03T19:12:54Z")

</div>

Hello,

I have an enum like

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

```

I can check that `apple` is a `Fruit`

```julia
julia> apple isa Fruit
true

```

But can’t check if 3 is an “allowed” value (ie a value which is used by this enum)

```julia
julia> 4 isa Fruit
false

julia> 3 isa Fruit
false

```

I tried `3 in Fruit` or `3 in keys(Fruit)` but enum are not iterable.

I haven’t dive into Enum internals… but maybe someone here can help.

(and docstring could probably be improved)

> <https://github.com/JuliaLang/julia/blob/master/base/Enums.jl#L95>

Kind regards

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 3, 2019, 7:57pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-in-an-enum/31815/2 "2019-12-03T19:57:25Z")

</div>

You could use the `instances` function.

For example, you can define

```julia
const fruitvals = Set(Int(f) for f in instances(Fruit))

```

and then do

```julia
julia> 3 in fruitvals
true

julia> 17 in fruitvals
false

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [December 3, 2019, 7:58pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-in-an-enum/31815/3 "2019-12-03T19:58:34Z")

</div>

If you need something generic, the only real way I can see is the ugly

```julia
julia> function ismember(enum::Type{T}, v::Int) where T<:Enum
           try
               T(v)
               return true
           catch
               return false
           end
       end
ismember (generic function with 1 method)

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

julia> ismember(Fruit, 1)
true

julia> ismember(Fruit, 4)
false

```

The list of available values ([https://github.com/JuliaLang/julia/blob/29b47b8a10397324a820717af655cb0ee1d7f023/base/Enums.jl#L133](https://github.com/JuliaLang/julia/blob/29b47b8a10397324a820717af655cb0ee1d7f023/base/Enums.jl#L133)) is not easily available (edit: nvm, see below).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 3, 2019, 8:02pm UTC](https://discourse.julialang.org/t/check-if-a-value-is-in-an-enum/31815/4 "2019-12-03T20:02:15Z")

</div>

You can also use the undocumented internal function `Base.Enums.namemap`:

```julia
julia> 4 in keys(Base.Enums.namemap(Fruit))
false

```

and there are also the `typemin` and `typemax` functions to get the minimum and maximum allowed values:

```julia
julia> Int.((typemin(Fruit), typemax(Fruit)))
(1, 3)

```
