# Defining multiple methods for same function within single module: how to export

**URL:** <https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 13, 2018, 5:35pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485 "2018-11-13T17:35:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [November 13, 2018, 5:35pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485/1 "2018-11-13T17:35:22Z")

</div>

I have a module `MyTest` which has a function `f` with two methods. From the REPL, I can define and extend a function `f` in successive definitions. Now I want to export `f` in my module so that both methods are exposed when I call `using MyTest`. Below is the example:

`funcs.jl` file:

```julia
function f(x::Number)
    return x^2
end
## f (generic function with 1 method)

function f(x::Number, p::Integer)
    return x^p
end
## f (generic function with 2 methods)

```

`MyTest.jl` file:

```julia
module MyTest
include("funcs.jl")
export f
end # module

```

When I run `using MyTest`, I get the following error:  
`WARNING: Method definition #f(Any, typeof(MyTest.f)) in module MyTest overwritten.`  
What am I missing? Thanks!

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [November 13, 2018, 6:35pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485/2 "2018-11-13T18:35:35Z")

</div>

I think the name of the module in the file `MyTest.jl` should be `MyTest` as the `Main` module of julia has a `Test` module already.

And, I copy-pasted your code in `funcs.jl` file and `MyTest.jl` file as you did. After writing the files, you should also include the file `MyTest.jl` file to define the module in REPL and than write `using MyTest` in the REPL if you are using julia-0.6 or `using Main.MyTest` if you are using julia-1.0. Than no warnings appear on the console.

Here is what I did.

`funcs.jl` file

```julia
function f(x::Number)
    return x^2
end
## f (generic function with 1 method)

function f(x::Number, p::Integer)
    return x^p
end
## f (generic function with 2 methods)

```

`MyTest.jl` file

```julia
module MyTest
include("funcs.jl")
export f
end # module

```

On julia0.6

```julia
julia> include("MyTest.jl")
MyTest

julia> using MyTest

julia> f
f (generic function with 2 methods)

julia> f(2)
4

julia> f(2,3)
8

```

On julia1.0

```julia

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

julia> using Main.MyTest

julia> f
f (generic function with 2 methods)

julia> f(2)
4

julia> f(2,3)
8

```

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [November 13, 2018, 6:45pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485/3 "2018-11-13T18:45:43Z")

</div>

> [@zekeriya.sari](#):
>
> using Main.MyTest

Ah, thanks, yes it should by `MyTest` (I forgot to change in the code I posted).

Why do I need to `include` first?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 13, 2018, 6:46pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485/4 "2018-11-13T18:46:05Z")

</div>

You should be able to do `using .MyTest` (note the dot before the module name) on Julia 0.6 or 1.0. That will also work even if the module happens to be named `Test`.

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [November 13, 2018, 7:14pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485/5 "2018-11-13T19:14:36Z")

</div>

> [@jacob-roth](#):
>
> Why do I need to `include` first?

See the error if we do not include it first.

```julia
julia> using MyTest
ERROR: ArgumentError: Package MyTest not found in current path:
- Run `Pkg.add("MyTest")` to install the MyTest package.

```

At this point, the module `MyTest` could not be found in the load path of julia. Note that in the docs of julia it is written that,

> [@](#):
>
> Given the statement `using Foo` , the system consults an internal table of top-level modules to look for one named `Foo` . If the module does not exist, the system attempts to `require(:Foo)` , which typically results in loading code from an installed package.  
> …  
> The global variable [`LOAD_PATH`](https://docs.julialang.org/en/v1/base/constants/#Base.LOAD_PATH) contains the directories Julia searches for modules when calling `require` .

Thus the module `MyTest` is not an installed package that julia can find by searching in the `LOAD_PATH` . To use it in the REPL, we must first define it in the REPL by loading the contents of the file `MyTest.jl` to REPL via the command `include("MyTest.jl")` and then we can use it via the command `using MyTest`.

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [November 13, 2018, 7:55pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485/6 "2018-11-13T19:55:48Z")

</div>

Ah, I see. I typically do `push!(LOAD_PATH, "~/path-to-MyTest/")`, and this way wasn’t working for me. Is one way preferred?

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [November 13, 2018, 8:10pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485/7 "2018-11-13T20:10:20Z")

</div>

It worked when I tried. The files `funcs.jl` and `MyTest.jl` are in the directory whose absolute path is `/home/sari/Desktop/temp`. So what I did is as follows

```julia
julia> push!(LOAD_PATH, "/home/sari/Desktop/temp")
4-element Array{String,1}:
 "@"
 "@v#.#"
 "@stdlib"
 "/home/sari/Desktop/temp"

julia> using MyTest
[Info: Recompiling stale cache file /home/sari/.julia/compiled/v1.0/MyTest.ji for MyTest [top-level]

julia> f(2,3)
8

```

Note that the absolute path must be given explicitly as `/home/sari/Desktop/temp`. `~/Desktop/temp` does not work as shown below.

```julia
julia> push!(LOAD_PATH, "~/Desktop/temp")
4-element Array{String,1}:
 "@"
 "@v#.#"
 "@stdlib"
 "~/Desktop/temp"

julia> using MyTest
ERROR: ArgumentError: Package MyTest not found in current path:
- Run `Pkg.add("MyTest")` to install the MyTest package.

```

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [November 14, 2018, 10:42pm UTC](https://discourse.julialang.org/t/defining-multiple-methods-for-same-function-within-single-module-how-to-export/17485/8 "2018-11-14T22:42:27Z")

</div>

Ah, yes this works. My error was in another attempt trying to use named parameters. Thanks!
