Automatic conversion with convert()

question

#1

I have the following code:

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.


#2
Base.convert(::Type{String}, x::Foo) = x.foo

Or

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

#3

Aah, I see, thanks @mohamed82008!