# Writing functions for types defined in another module

**URL:** <https://discourse.julialang.org/t/writing-functions-for-types-defined-in-another-module/31895>\
**Category:** Performance\
**Created:** [December 5, 2019, 10:19am UTC](https://discourse.julialang.org/t/writing-functions-for-types-defined-in-another-module/31895 "2019-12-05T10:19:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [December 5, 2019, 10:19am UTC](https://discourse.julialang.org/t/writing-functions-for-types-defined-in-another-module/31895/1 "2019-12-05T10:19:28Z")

</div>

I want to have type definitions in one module `module1` and then write functions that operate on my type in another module `module2` and use these functions and the type in my main script / REPL. But in my current setup I then get `MethodError: no method matching f(::mytype) Closest candidates are: f(!Matched::Main.module2.module1.mytype) `

Here is the full MWE:

`module1.jl`

```julia
module module1

export mytype

mutable struct mytype
	x::Int
end

end #module

```

`module2.jl`

```julia
module module2

include("./module1.jl")
using .module1

export f

function f(a::mytype)
	println(a.x)
end

end # module

```

and finally the test script:

`testscript.jl`

```julia
include("./module1.jl")
include("./module2.jl")
using .module1
using .module2

a = mytype(5)
f(a)

```

and if I then run `julia testscript.jl` I get the error message

```julia
ERROR: LoadError: MethodError: no method matching f(::mytype)
Closest candidates are:
  f(!Matched::Main.module2.module1.mytype)

```

which sounds an awful lot like julia thinks, that `mytype` in the MAIN scope and in the `module2` scope are different types. How do I have to setup the includes / using to make it clear, that both are the same thing?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [December 5, 2019, 10:44am UTC](https://discourse.julialang.org/t/writing-functions-for-types-defined-in-another-module/31895/2 "2019-12-05T10:44:54Z")

</div>

I think this is a discussion of a similar question:

> [@What is the preferred way to use multiple files?](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687):
>
> I wanted to structure my code by dividing it into several files. But I ran into some problem. So, in its simplest form, I have 3 files: main.jl module main include("structs.jl") include("functions.jl") first\_point = structs.Point(1.5, 1.5) print(functions.distance(first\_point)) end functions.jl module functions include("structs.jl") export distance function distance(point) return (point.x^2+point.y^2)^0.5 end end structs.jl module structs export Point mutable struct Point…

---

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [December 5, 2019, 10:57am UTC](https://discourse.julialang.org/t/writing-functions-for-types-defined-in-another-module/31895/3 "2019-12-05T10:57:23Z")

</div>

It is similar, but the accepted answer there has one problem: The accepted solution in that thread is to remove `include("./module1") \n using .module1` in the main script and write `f(a) = ...` instead of `f(a::mytype) = ...` because there is no `mytype` inside the scope of `module2` now.

But what do I do if I want to explcitely specify the argument type of `f` to be `mytype`? It seems, that in that case I definitely have to include `module1` somehow in `module2`.

---

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [December 5, 2019, 11:12am UTC](https://discourse.julialang.org/t/writing-functions-for-types-defined-in-another-module/31895/4 "2019-12-05T11:12:21Z")

</div>

Okay, got it fixed now, though I am still not clear _why_ it works now… So if someone can elaborate on that, it would be greatly appreciated. But here is the current, working setup:

The `module1` with the types is unchanged:  
`module1.jl`

```julia
module module1

export mytype

mutable struct mytype
	x::Int
end

end #module

```

In `module2` I had to replace `include("./module1.jl") \n using .module1` with `using ..module1` (why??)

`module2.jl`

```julia
module module2

#include("./module1.jl")
using ..module1

export f

function f(a::mytype)
	println(a.x)
end

end # module

```

And in the test script I have to make sure that I include `module1` before `module2`

`testscript.jl`

```julia
include("./module1.jl")
include("./module2.jl")
using .module2
using .module1

a = mytype(5)
f(a)

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [December 5, 2019, 11:29am UTC](https://discourse.julialang.org/t/writing-functions-for-types-defined-in-another-module/31895/5 "2019-12-05T11:29:17Z")

</div>

```julia
using .SomeModule

```

means “use SomeModule within the current module scope”,

```julia
using ..SomeModule

```

means “use SomeModule within the parent module scope”, etc. (more dots - going more levels up).

The initial issue was because including the same file within two different modules imports the definitions within that file into different namespaces.

---

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [December 5, 2019, 11:51am UTC](https://discourse.julialang.org/t/writing-functions-for-types-defined-in-another-module/31895/6 "2019-12-05T11:51:51Z")

</div>

Nice, thanks!
