Please explain the following example.
julia> string_foo = "foo"
"foo"
julia> eval(string_foo)
"foo"
julia> eval(:string_foo)
"foo"
julia> a_dict = Dict(:string_foo => 1, "string_foo" => 2,string_foo => 3)
Dict{Any,Int64} with 3 entries:
"string_foo" => 2
:string_foo => 1
"foo" => 3
Not so surprising results:
julia> a[string_foo]
3
julia> a["string_foo"]
2
julia> a[:string_foo]
1
More not so surprising results:
julia> a[eval(string_foo)]
3
julia> a[eval("string_foo")]
2
Now please explain this one!
julia> a[eval(:string_foo)]
3
My hesitant conclusion is that when a variable (foo
) the same as symbol (:foo
) is defined, then one can use it as a key (:foo
) or as a value (eval(:foo)
)
This means that I could do the following before defining any variables at all:
julia> list_template = [:el1, :el2, :el3]
3-element Array{Symbol,1}:
:el1
:el2
:el3
julia> el1 = 1; el2 = 2; el3 = 3;
julia> eval(list_template)
3-element Array{Symbol,1}:
:el1
:el2
:el3
julia> eval.(list_template)
3-element Array{Int64,1}:
1
2
3
julia> el1 = "one"; el2 = "two"; el3 = "three";
julia> eval(list_template)
3-element Array{Symbol,1}:
:el1
:el2
:el3
julia> eval.(list_template)
3-element Array{String,1}:
"one"
"two"
"three"
That seems like it could be useful in some cases. What other uses do Julia Symbols have?