# Cannot convert despite convert method being defined

**URL:** <https://discourse.julialang.org/t/cannot-convert-despite-convert-method-being-defined/88813>\
**Category:** General Usage\
**Tags:** type, convert\
**Created:** [October 16, 2022, 8:11pm UTC](https://discourse.julialang.org/t/cannot-convert-despite-convert-method-being-defined/88813 "2022-10-16T20:11:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Post date:** [October 16, 2022, 8:11pm UTC](https://discourse.julialang.org/t/cannot-convert-despite-convert-method-being-defined/88813/1 "2022-10-16T20:11:47Z")

</div>

I have the following sample code:

```julia
struct Bar
    b:: Int64
end

convert(::Type{Bar}, x::Int64) = Bar(x)

struct Foo
    b::Bar
end

```

I execute it in the REPL. I then write

`f = Foo(1)`

I expect it to work because I have defined how to implicitly convert from and Int64 to Bar. However I get the error:

`ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Bar`

Why doesn’t this work?

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [October 16, 2022, 8:17pm UTC](https://discourse.julialang.org/t/cannot-convert-despite-convert-method-being-defined/88813/2 "2022-10-16T20:17:27Z")

</div>

```julia
Base.convert(::Type{Bar}, x::Int64) = Bar(x)

```

You were creating a new function called `convert` instead of adding a method to `Base.convert`. You either need to fully qualify the function name or do `import Base: convert` first.
