# Using modules causes isa() to return the incorrect result

**URL:** https://discourse.julialang.org/t/using-modules-causes-isa-to-return-the-incorrect-result/61028
**Category:** General Usage
**Created:** [May 12, 2021, 3:18pm UTC](https://discourse.julialang.org/t/using-modules-causes-isa-to-return-the-incorrect-result/61028 "2021-05-12T15:18:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)
#### Post date: [May 12, 2021, 3:18pm UTC](https://discourse.julialang.org/t/using-modules-causes-isa-to-return-the-incorrect-result/61028/1 "2021-05-12T15:18:05Z")

</div>

```julia
# GameObj.jl
module GameObj
export GameObject
abstract type GameObject end
end

```

```julia
# WeaponObj.jl
module WeaponObj
export Weapon
include("GameObj.jl")
using .GameObj
abstract type Weapon <: GameObject end
end

```

```julia
# ChargeGunObj.jl
module ChargeGunObj
export ChargeGun
include("WeaponObj.jl")
using .WeaponObj

struct ChargeGun <: Weapon
    number_of_charges::Int32
    damage::Int32

    function ChargeGun()
        number_of_charges = 10
        damage = 5
        new(number_of_charges,damage)
    end
end
end

```

```julia
using .ChargeGunObj
using .WeaponObj

function main()

    cg :: Weapon = ChargeGun()
    println(cg.damage)

end

main()

```

The problem is when I use `Weapon` as a type annotation, it returns:

```julia
LoadError: MethodError: Cannot `convert` an object of type ChargeGun 
                        to an object of type Weapon

```

and `isa()` returns the incorrect result:

```julia
cg = ChargeGun()
println(isa(cg,Weapon)) # returns false, despite the fact that ChargeGun is a Weapon.

```

If I were to remove the modules and write everything out in the `main.jl,` it works.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 12, 2021, 3:28pm UTC](https://discourse.julialang.org/t/using-modules-causes-isa-to-return-the-incorrect-result/61028/2 "2021-05-12T15:28:13Z")

</div>

The problem is that you accidentally define two different modules named `WeaponObj`. One is `Main.WeaponObj`, and one is `Main.ChargeGunObj.WeaponObj`. Your codes give something like this tree:

```nohighlight
Main
├── ChargeGunObj
│ ├── ChargeGun <: Main.ChargeGunObj.WeaponObj.Weapon
│ └── WeaponObj
│ └── Weapon
└── WeaponObj
    └── Weapon

```

where, in your `main` function create a `Main.ChargeGunObj.ChargeGun` (which is a subtype of `Main.ChargeGunObj.WeaponObj.Weapon` but check if it is a subtype of `Main.WeaponObj.Weapon`. (which, while they have the same name, are not the same type, i.e. `Main.ChargeGunObj.WeaponObj.Weapon` !== `Main.WeaponObj.Weapon`).

The rule is to only `include` the same file once.

---

<div class="post-metadata">

### Author: ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)
#### Post date: [May 12, 2021, 3:43pm UTC](https://discourse.julialang.org/t/using-modules-causes-isa-to-return-the-incorrect-result/61028/3 "2021-05-12T15:43:25Z")

</div>

I’m a little confused about how to fix it, do I remove the `using .WeaponObj` and just leave `using .ChargeGun`?

If I do that, `isa()` still returns `false`, and I can’t do `cg :: Weapon = ChargeGun()` because `WeaponObj` isn’t available.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 12, 2021, 4:04pm UTC](https://discourse.julialang.org/t/using-modules-causes-isa-to-return-the-incorrect-result/61028/4 "2021-05-12T16:04:16Z")

</div>

How about something like this:

```nohighlight
$ cat GameObj.jl 
module GameObj
    export GameObject
    abstract type GameObject end
end # module

$ cat WeaponObj.jl 
module WeaponObj
    export Weapon
    # Use .. to look for GameObj in the parent module
    # instead of including the file
    using ..GameObj
    abstract type Weapon <: GameObject end
end

$ cat ChargeGunObj.jl 
module ChargeGunObj
    export ChargeGun
    using ..WeaponObj

    struct ChargeGun <: Weapon
        number_of_charges::Int32
        damage::Int32

        function ChargeGun()
            number_of_charges = 10
            damage = 5
            new(number_of_charges, damage)
        end
    end
end

$ cat Game.jl 
include("GameObj.jl")
include("WeaponObj.jl")
include("ChargeGunObj.jl")

using .GameObj, .WeaponObj, .ChargeGunObj

function main()
    cg :: Weapon = ChargeGun()
    println(cg.damage)
end

main()

$ julia Game.jl
5

```
