How can I get the name of an enum value?
@enum Animal dog cat
Int(cat) # works
String(cat) # not working
How can I get the string representation of a variable of type Animal?
I want to get the string “cat” or “dog”.
How can I get the name of an enum value?
@enum Animal dog cat
Int(cat) # works
String(cat) # not working
How can I get the string representation of a variable of type Animal?
I want to get the string “cat” or “dog”.
You need to use Symbol to get the name. That can then be converted to a string though
julia> @enum Animal dog cat
julia> Symbol(cat)
:cat
julia> String(Symbol(cat))
"cat"
Thanks a lot!
Any idea why the direct conversion is not implemented?
The function string(x) appeared in Julia long before the type String.
The type String appeared in advance of the deep move from types with constructors to types as constructors. And string was present to be the “constructor method” that delivers T<:AbstractString.
strx = string(x) does “construct” strx from x, and
expect isa(strx, AbstractString).
using Test
a, stra = 1, "1"
@test string(a) == stra
@test_throws MethodError String(a)