# Correct use of convert

**URL:** <https://discourse.julialang.org/t/correct-use-of-convert/50096>\
**Category:** General Usage\
**Tags:** convert\
**Created:** [November 13, 2020, 3:48pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096 "2020-11-13T15:48:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [November 13, 2020, 3:48pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/1 "2020-11-13T15:48:09Z")

</div>

The last line in the program below fails with the error

_ERROR: LoadError: MethodError: Cannot `convert` an object of type String to an object of type Symbol_

The message is misleading, because it certainly can do such a conversion as the line before it demonstrates. More importantly, how do I write the convert statement in such a way that the last statement works? Thanks!

```julia
convert(::Type{Symbol}, x::String) = Symbol(x)
b = Vector{Symbol}(undef,2)
println( convert( Symbol, "hi" ) )
b[:] = ["hi"; "hello"]

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [November 13, 2020, 3:52pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/2 "2020-11-13T15:52:47Z")

</div>

Does using `Base.convert` work?

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [November 13, 2020, 3:53pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/3 "2020-11-13T15:53:42Z")

</div>

No. That already fails on line 3.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [November 13, 2020, 3:55pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/4 "2020-11-13T15:55:47Z")

</div>

I meant the method definition

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [November 13, 2020, 3:56pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/5 "2020-11-13T15:56:49Z")

</div>

ha, it does! Thanks!! I understand what’s happening. Gee. That’s easy.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 13, 2020, 4:06pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/6 "2020-11-13T16:06:46Z")

</div>

Also note that you should not define this method for `Base.convert`. It’s type piracy and can break code for everyone else.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [November 13, 2020, 4:36pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/7 "2020-11-13T16:36:45Z")

</div>

Thanks @yuyichao .

Ok, how could I address this issue without committing type piracy?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 13, 2020, 4:54pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/8 "2020-11-13T16:54:49Z")

</div>

By making sure to turn the data you’re writing into `b` into `Symbol`s first. E.g.

```julia
b .= Symbol.(["hi", "hello"])

```

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [November 13, 2020, 4:56pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/9 "2020-11-13T16:56:10Z")

</div>

Thanks and sure.

But that’s not the question I meant to ask. What I meant to ask was: How can I get this conversion to work automatically?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 13, 2020, 5:08pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/10 "2020-11-13T17:08:32Z")

</div>

You should no do that.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 13, 2020, 5:23pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/11 "2020-11-13T17:23:52Z")

</div>

Make your own type so it’s not piracy 🤷‍♂️

```julia
struct MyVector{T} <: AbstractVector{T}
    v::Vector{T}
end

# (among many other methods...)
Base.setindex!(v::MyVector{T}, x, i) = v.v[i] = T(x)

```

```julia
julia> MV = MyVector(Symbol[:a, :b, :c]);

julia> MV.v
3-element Array{Symbol,1}:
 :a
 :b
 :c

julia> MV[2] = "q"
"q"

julia> MV.v
3-element Array{Symbol,1}:
 :a
 :q
 :c

```

I would look for the most obvious way to make do without the automatic conversion, as @yuyichao suggests…

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 13, 2020, 6:11pm UTC](https://discourse.julialang.org/t/correct-use-of-convert/50096/12 "2020-11-13T18:11:36Z")

</div>

There’s a school of thought that type conversions should not occur automatically/implicitly, and some languages do not support implicit conversions. Clearly the designer’s of Julia do not adhere to that, but gratuitous implicit conversions can lead to confusing, abstruse or erroneous code. See javascript, for example.
