# Functions in a Module can't resolve convert routines defined in the module?

**URL:** https://discourse.julialang.org/t/functions-in-a-module-cant-resolve-convert-routines-defined-in-the-module/6205
**Category:** New to Julia
**Tags:** question
**Created:** [October 2, 2017, 8:31pm UTC](https://discourse.julialang.org/t/functions-in-a-module-cant-resolve-convert-routines-defined-in-the-module/6205 "2017-10-02T20:31:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [October 2, 2017, 8:31pm UTC](https://discourse.julialang.org/t/functions-in-a-module-cant-resolve-convert-routines-defined-in-the-module/6205/1 "2017-10-02T20:31:47Z")

</div>

Hi all,

I’m writing my first non-trivial Julia project. I’ve run into a problem I can’t quite figure out. I want to use quaternions from the Quaternion package and send them to a C library that takes quaternions as a four element Float32 array. So I’ve written the following convert routine:

```julia
unsafe_convert(::Type{Ptr{Float32}}, q::Quaternions.Quaternion{Float64}) =
    convert(Ptr{Float32}, Ptr{Float32}(pointer_from_objref([imag(q), real(q)])))

```

which is declared inside the module that needs it. This code works fine from the REPL, I get the following:

```julia
julia> q = Quaternion(1.0, 0.0, 0.0, 0.0)
1.0 + 0.0im + 0.0jm + 0.0km

julia> x = Ptr{Float32}
Ptr{Float32}

julia> unsafe_convert(x, q)
Ptr{Float32} @0x0000000113d2a890

```

But when I put it in the module and try to use I get the following:

```julia
julia> include("distance_example.jl")
WARNING: replacing module Distance
ERROR: LoadError: MethodError: no method matching unsafe_convert(::Type{Ptr{Float32}}, ::Quaternions.Quaternion{Float64})
Closest candidates are:

```

---

<div class="post-metadata">

### Author: ![ararslan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ararslan/32/3825_2.png) [@ararslan](https://discourse.julialang.org/u/ararslan)
#### Post date: [October 2, 2017, 8:34pm UTC](https://discourse.julialang.org/t/functions-in-a-module-cant-resolve-convert-routines-defined-in-the-module/6205/2 "2017-10-02T20:34:39Z")

</div>

When you defined your method for `unsafe_convert`, did you extend the method from Base? If not, try defining your method as `Base.unsafe_convert(...) = ...`, i.e. qualifying the call so that Julia knows to overload `unsafe_convert` rather than define a new local function called `unsafe_convert`.

---

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [October 2, 2017, 8:37pm UTC](https://discourse.julialang.org/t/functions-in-a-module-cant-resolve-convert-routines-defined-in-the-module/6205/3 "2017-10-02T20:37:24Z")

</div>

OK, that worked. Thanks!

Why did it work, though? Julia doesn’t know to automatically call a locally defined unsafe\_convert to do a conversion?

---

<div class="post-metadata">

### Author: ![ararslan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ararslan/32/3825_2.png) [@ararslan](https://discourse.julialang.org/u/ararslan)
#### Post date: [October 2, 2017, 8:46pm UTC](https://discourse.julialang.org/t/functions-in-a-module-cant-resolve-convert-routines-defined-in-the-module/6205/4 "2017-10-02T20:46:35Z")

</div>

No, since conversions call `Base.unsafe_convert`, and what you’ve (implicitly) defined is `Main.unsafe_convert`. In general names aren’t significant unless you’re extending methods from other modules.

---

<div class="post-metadata">

### Author: ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)
#### Post date: [October 2, 2017, 8:48pm UTC](https://discourse.julialang.org/t/functions-in-a-module-cant-resolve-convert-routines-defined-in-the-module/6205/5 "2017-10-02T20:48:07Z")

</div>

Julia doesn’t extend another package’s functions unless you explicitly tell it to by qualifying like `Base.unsafe_convert` or having an `import Base: unsafe_convert` somewhere. If you don’t tell julia to extend the function than you’re defining a new function with the same name inside your package.
