# Automatic conversion with convert()

**URL:** https://discourse.julialang.org/t/automatic-conversion-with-convert/9358
**Category:** General Usage
**Tags:** question
**Created:** [February 27, 2018, 3:42am UTC](https://discourse.julialang.org/t/automatic-conversion-with-convert/9358 "2018-02-27T03:42:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Robert\_Honig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robert_honig/32/4930_2.png) [@Robert\_Honig](https://discourse.julialang.org/u/Robert_Honig)
#### Post date: [February 27, 2018, 3:42am UTC](https://discourse.julialang.org/t/automatic-conversion-with-convert/9358/1 "2018-02-27T03:42:55Z")

</div>

I have the following code:

```julia
struct Foo
    foo::String
end
convert(::Type{String}, x::Foo) = x.foo
String(Foo("bar"))

```

This gives me the following error:

> ERROR: MethodError: Cannot `convert` an object of type Foo to an object of type String  
> This may have arisen from a call to the constructor String(…),  
> since type constructors fall back to convert methods.

Shouldn’t this work? From my understanding, convert() should cause an automatic conversion of the Foo object to String, if no matching constructor is found.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [February 27, 2018, 3:46am UTC](https://discourse.julialang.org/t/automatic-conversion-with-convert/9358/2 "2018-02-27T03:46:56Z")

</div>

```julia
Base.convert(::Type{String}, x::Foo) = x.foo

```

Or

```julia
import Base: convert
convert(::Type{String}, x::Foo) = x.foo

```

---

<div class="post-metadata">

### Author: ![Robert\_Honig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robert_honig/32/4930_2.png) [@Robert\_Honig](https://discourse.julialang.org/u/Robert_Honig)
#### Post date: [February 27, 2018, 1:49pm UTC](https://discourse.julialang.org/t/automatic-conversion-with-convert/9358/3 "2018-02-27T13:49:58Z")

</div>

Aah, I see, thanks @mohamed82008!
