# Get the enumerate type components from the string counterpart

**URL:** <https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083>\
**Category:** General Usage\
**Tags:** enum\
**Created:** [April 12, 2019, 3:07pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083 "2019-04-12T15:07:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [April 12, 2019, 3:07pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/1 "2019-04-12T15:07:53Z")

</div>

This is a simple task. I tried the following and it did not work.

```julia
julia> @enum Fruit apple=0 pear=1 banana=2 
julia> Fruit("apple")
ERROR: MethodError: no method matching Fruit(::String)
Closest candidates are:
  Fruit(::Integer) at Enums.jl:134
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 12, 2019, 3:10pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/2 "2019-04-12T15:10:34Z")

</div>

Is there a reason to avoid the enum’s names?

```julia
julia> @enum Fruit apple=0 pear=1

julia> apple
apple::Fruit = 0

julia> pear
pear::Fruit = 1

```

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [April 12, 2019, 3:11pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/3 "2019-04-12T15:11:28Z")

</div>

I have a string, say, “banana”, I’d like to change it back to the corresponding enumerate type…

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 12, 2019, 3:17pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/4 "2019-04-12T15:17:22Z")

</div>

without error checking

```julia
julia> function fruit(s::String)
          [apple, pear][s .== ["apple", "pear"]][1]
       end
fruit (generic function with 1 method)

julia> fruit("pear")
pear::Fruit = 1

```

or

```julia
function fruit(s::String)
      Fruit( (0:1)[s .== ["apple", "pear"]][1] )
end

```

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [April 12, 2019, 3:19pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/5 "2019-04-12T15:19:14Z")

</div>

Good. Are there default functions that I can use directly? By the way, what is `s .==`?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 12, 2019, 3:23pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/6 "2019-04-12T15:23:00Z")

</div>

`s .== [a, b]` is `[s === a, s === b]`  
`s .= [a, b]` is `[s == a, s == b]`

Default functions for what purpose?

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [April 12, 2019, 3:23pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/7 "2019-04-12T15:23:40Z")

</div>

for transfer a string to the enumerate type counterpart

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 12, 2019, 3:25pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/8 "2019-04-12T15:25:52Z")

</div>

not entirely

```julia
julia> instances(Fruit)
(apple, pear)

julia> string.(instances(Fruit))
("apple", "pear")

```

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [April 12, 2019, 3:30pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/9 "2019-04-12T15:30:38Z")

</div>

what you did above is transferring the enumerate to string counterpart.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 12, 2019, 3:36pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/10 "2019-04-12T15:36:37Z")

</div>

you can do this

```julia
const fruits = instances(Fruit)
const fruitvalues = Int32.(fruits)
const fruitstrings = string.(fruits)
fruit(s::String) = Fruit(fruitvalues[[(s .== fruitstrings)...]][1])

fruit("apple")
apple::Fruit == 0

```

---

<div class="post-metadata">

**Author:** ![MatthijsCox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthijscox/32/42831_2.png) [@MatthijsCox](https://discourse.julialang.org/u/MatthijsCox)\
**Post date:** [March 23, 2020, 3:14pm UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/11 "2020-03-23T15:14:07Z")

</div>

I also would like this kind of functionality for all my Enums, but I don’t like declaring constants for every enum.

I’ve arrived so far on the solution below. But I should probably write my own enum macro, similar to [this discussion on enum dot access](https://discourse.julialang.org/t/encapsulating-enum-access-via-dot-syntax/11785):

```julia
function get_instance(enum::DataType, str::String)
	idx = findfirst(x -> x==str, instance_names(enum))
	if idx == nothing
		throw(ArgumentError("invalid value for Enum $enum: $str"))
	else
		return enum(idx-1)
	end
end
instance_names(enum::DataType) = string.(instances(enum))

@enum Fruits begin
    apple
	pear
	banana
end
Fruits(str::String) = get_instance(Fruits, str)
Base.convert(::Type{Fruits}, str::String) = Fruits(str)

```

Now this works fine:

```julia
julia> Fruits("banana")
banana::Fruits = 2

julia> Fruits("monkey")
ERROR: ArgumentError: invalid value for Enum Fruits: monkey

```

I had to add the Base.convert since I am creating an enum instance from a constructor:

```julia
struct FruitBowl
   fruit::Fruits
end

julia> bowl = FruitBowl("banana")
julia> bowl.fruit
banana::Fruits = 2

```

I do not know why I could not dispatch on the Enum type though. typeof(Fruits) returned Datatype, not Enum.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 25, 2020, 1:42am UTC](https://discourse.julialang.org/t/get-the-enumerate-type-components-from-the-string-counterpart/23083/12 "2020-03-25T01:42:48Z")

</div>

… because you tried taking the type of the Enum rather than the type of an instance of it:

```julia
julia> typeof(Fruits)
DataType

julia> typeof(bowl.fruit)
Enum Fruits:
apple = 0
pear = 1
banana = 2

```
