Since the Symbol
constructor works, is there a reason why convert(Symbol, "foo")
shouldn’t work?
julia> Symbol("foo")
:foo
julia> convert(Symbol, "foo")
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Symbol
Since the Symbol
constructor works, is there a reason why convert(Symbol, "foo")
shouldn’t work?
julia> Symbol("foo")
:foo
julia> convert(Symbol, "foo")
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Symbol
convert
happens automatically inside of the standard constructor.
julia> struct Foo
a::Symbol
end
julia> Foo("lion")
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Symbol
Closest candidates are:
convert(::Type{T}, ::T) where T at essentials.jl:171
Symbol(::String) at boot.jl:436
Symbol(::AbstractString) at strings/basic.jl:226
...
Stacktrace:
[1] Foo(::String) at ./REPL[1]:2
[2] top-level scope at REPL[2]:1
would work if convert
was defined. Presumably, the Julia developers judged that conversion to/from strings should be done explicitly instead. I’m sure other language designers would have chosen otherwise, but personally I agree with the choice…