User-defined type used in a module differs from the original

I am a newbie with Julia (thigh not with programming in general).
Asn exercise i wanted to some simple 3D geometry modules.

One module for 3D vectors (Vec3D) which includes an overloaded operator “^” for the outer product::

function Base.:^(a::Vec3D, b::Vec3D) 
    Vec3D(a.y*b.z - a.z*b.y,   
                a.z+b.x - a.x*b.z,
               a.x*b.y - a.z*b.y)
end

I then have module for quaternions (Quats)with an operator ^ used for applying a quaternion to a 3D vector:

function Base.:^(a::Quat, b::Vec3D) 
    ...
end

For this to work, i had to include the file containing the Vec3D definitions and import them:

include("vectors3d.jl")
import .Vectors3D:Vec3D

Now, when in julia i include the two files of he the modules, almost everything works as expected. However i have a problem with the quaternions´ “^”-operator;

julia> include("vectors3d.jl")
Main.Vectors3D

julia> include("quats.jl")
Main.quats

julia> using .Vectors3D

julia> using .Quats

julia> a=Vec3D(1,0,0)
Vec3D(1, 0, 0)

julia> q=Quat(0, 0, 0, 1)
Quat(0, 0, 0, 1)

julia> q^a
ERROR: MethodError: no method matching ^(::Quat, ::Vec3D)
Closest candidates are:
    ^(::Quat, ::Main.Quats.Vectors3D.Vec3D) at ~/progs/jl_progs/quats.jl:126
    ^(::Vec3D, ::Vec3D) at ~/progs/jl_progs/vectors3d.jl:50
Stacktrace:
 [1] top-level scope
   @ REPL[8]:1

It looks as if the type of the Vec3D i included and used inside the Quat module is seen as a type different from the Vec3D in the Vectors3D module.

When i define the “^”-operator directly in julia, it works perfectly.

How can i convince julia that the Vec3D type used inside the Quatsmodule is the same as the one from the Vectors3D

Import it in the Quats module. You can make this module dependent on the other or, maybe better, make both depend on a third that defines the type.

1 Like