Yep, it is only exported on master
.
Q: I understand that we have both strings and symbols, and how to convert between them. (And, in many older languages, strings take the place of symbols, too.)
Other than type and notation, are there differences between them? I am guessing that any symbol can be converted into a string. is the opposite also true? any other differences one should be aware of.
Comprehensive explanation of the difference can be found here: What is a "symbol" in Julia? - Stack Overflow
Summarized:
Symbol equality is faster than String equality, since symbols are interned. In AST Symbols and Strings behave very differently, as can be seen when using eval.
julia> x = 5
5
julia> eval("x")
"x"
julia> eval(:x)
5
julia> eval(Meta.quot(:x))
:x
end of summery.
Yes, strings can be converted into symbols as well.
julia> string(:x)
"x"
julia> Symbol("x")
:x
Another difference to be aware of is that Symbols are not a subtype of AbstractString, amongst other functions they do not support they do not have length and can not be indexed into.
It has occurred to me that it is probably very confusing for newcomers that x.y
is lowered to getfield(x, :y)
, meanwhile x."y"
is lowered to getfield(x, "y")
. In getfield
operations symbols are treated specially in that you can call getfield
with them without further qualifying them as symbols, whereas in other contexts they must be explicitly annotated with :x
. Having spent a large amount of time messing around with Julia’s AST I find this fairly natural, but I imagine that if this feature existed when I was a newcomer to the language I would have been very confused by it.
And I have noticed that eval("x")
produces the same output than eval(x)
Well yes, if x == "x"
of course that would be the case.
But here x is not “x” but 5.
Huh?
julia> x = 5
5
julia> eval("x")
"x"
julia> eval(x)
5
The key is that Julia parses the text you give it before it eval
s it. So that’s the distinction between a String
and the general text at a REPL prompt or in a file.
julia> x = 5;
julia> Meta.parse(" x ")
:x
julia> eval(Meta.parse(" x "))
5
I think it might’ve been
x=5
eval(x) == eval(:x)