# Why do I get an error if I define the correct type for a function argument?

**URL:** <https://discourse.julialang.org/t/why-do-i-get-an-error-if-i-define-the-correct-type-for-a-function-argument/69498>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 10, 2021, 4:02am UTC](https://discourse.julialang.org/t/why-do-i-get-an-error-if-i-define-the-correct-type-for-a-function-argument/69498 "2021-10-10T04:02:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [October 10, 2021, 4:03am UTC](https://discourse.julialang.org/t/why-do-i-get-an-error-if-i-define-the-correct-type-for-a-function-argument/69498/1 "2021-10-10T04:03:00Z")

</div>

I managed to some code to work but there is something that puzzles me: if I declare the type of a function argument the code does not run and I get an error message.

The example below involves three different files (I could not do it with less than three files). The files are `Test1.jl`, `Test2.jl`, and `SomeModule.jl`. `Test1.jl` does not contain a module, only code. The other two files contain modules with the same names (without the “.jl”, of course).

In `SomeModule.jl` I define a struct:

```julia
module SomeModule

struct MyStruct
    A::Bool
end

end

```

`Test2.jl` defines a function. In this version no type is defined for the function argument.

```julia
cd("C:/Dropbox/Code/Julia/bootstrapwalkforward/BW/src")

module Test2

include("./SomeModule.jl");

import .SomeModule

function bar(a)
    if a.A
        println("AAAAA")
    end
end

end

```

Finally, `Test1.jl` calls the function:

```julia
cd("C:/Dropbox/Code/Julia/bootstrapwalkforward/BW/src")

include("./SomeModule.jl");
include("./Test2.jl");

import .SomeModule;
import .Test2

a = SomeModule.MyStruct(true)

Test2.bar(a)

```

The code above runs with no problems.

However, if the function definition’s first line is replaced witn

`function bar(a::SomeModule.MyStruct)`

then the code does not run, with this message:

```julia
julia> Test2.bar(a)
ERROR: MethodError: no method matching bar(::Main.SomeModule.MyStruct)
Closest candidates are:
  bar(::Main.Test2.SomeModule.MyStruct) at C:\Dropbox\Code\Julia\bootstrapwalkforward\BW\src\Test2.jl:9
Stacktrace:
 [1] top-level scope
   @ REPL[7]:1

```

I tried a variety of other alternatives for the first line of the function definition and none of them worked. Only having no type definition worked.

Any ideas on why is this happening?

---

<div class="post-metadata">

**Author:** ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)\
**Post date:** [October 10, 2021, 4:19am UTC](https://discourse.julialang.org/t/why-do-i-get-an-error-if-i-define-the-correct-type-for-a-function-argument/69498/2 "2021-10-10T04:19:53Z")

</div>

This is a common question for users new to Julia. The reason is, when you try to replace the the definition of `bar` in `Test2.jl` with

```julia
function bar(a::SomeModule.MyStruct)

```

The `SomeModule.MyStruct` here is referencing the module `SomeModule` you included in module `Test2`. Not the one you included in `Test1.jl`, althought they share the same name.

You could try `a = Test2.SomeModule.MyStruct` and call `Test2.bar(a)` instead here.

Usually, it’s not a good practice to include a file twice. I’d rather remove the `include("./SomeModule.jl")` in `Test1.jl` and run `import .Test2.SomeModule` instead.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 10, 2021, 4:28am UTC](https://discourse.julialang.org/t/why-do-i-get-an-error-if-i-define-the-correct-type-for-a-function-argument/69498/3 "2021-10-10T04:28:24Z")

</div>

include is equivalent to copy and paste the content of the included file in the same place. When you included the `SomeModule.jl` in the Test2 module, you created a `Test2.SomeModule.MyStruct`, and then you included the file in `Test1.jl`, you made a `Main.SomeModule.MyStruct`

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [October 10, 2021, 9:53am UTC](https://discourse.julialang.org/t/why-do-i-get-an-error-if-i-define-the-correct-type-for-a-function-argument/69498/4 "2021-10-10T09:53:11Z")

</div>

Your suggestion worked. I understand there is no need to include a file twice. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [October 10, 2021, 9:54am UTC](https://discourse.julialang.org/t/why-do-i-get-an-error-if-i-define-the-correct-type-for-a-function-argument/69498/5 "2021-10-10T09:54:19Z")

</div>

Thanks. I got it.
