# Julia modules

**URL:** <https://discourse.julialang.org/t/julia-modules/16878>\
**Category:** New to Julia\
**Created:** [October 27, 2018, 9:10pm UTC](https://discourse.julialang.org/t/julia-modules/16878 "2018-10-27T21:10:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheevy](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@sheevy](https://discourse.julialang.org/u/sheevy)\
**Post date:** [October 27, 2018, 9:10pm UTC](https://discourse.julialang.org/t/julia-modules/16878/1 "2018-10-27T21:10:50Z")

</div>

Hi,

I 'm having troubles understanding how to package my functionality into modules. I have a general algorithm which is expressed using number of generic functions (which different types will later provide implementation for). I wanted to put this algo into a separate module. Here’s an example:

```julia
module General
export algo
algo(x) = internals(x)
end

module Specific
export internals
internals(x) = x
end

module Test
using Main.General
using Main.Specific
algo(2)
end

```

algo is expressed in terms of a generic function internals.

However , when executing Test I’m getting:

```julia
ERROR: UndefVarError: internals not defined
Stacktrace:
 [1] algo(::Int64) at ./REPL[111]:3
 [2] top-level scope at none:0

```

I must be missing something embarrassingly simple and fundamental. What am I doing wrong?

Thanks

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 27, 2018, 9:22pm UTC](https://discourse.julialang.org/t/julia-modules/16878/2 "2018-10-27T21:22:18Z")

</div>

You need to tell `General` where to find `internals`:

```julia
       module General
       using ..Specific
       export algo
       algo(x) = internals(x)
       end                                                                                                                                                              

```

PS: read [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

**Author:** ![sheevy](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@sheevy](https://discourse.julialang.org/u/sheevy)\
**Post date:** [October 27, 2018, 11:29pm UTC](https://discourse.julialang.org/t/julia-modules/16878/3 "2018-10-27T23:29:34Z")

</div>

Thanks. Now I get a conflict when I try to include a second implementation of `internals` for a different type.

I changed my example I hope to be more clear of what I want to achieve:

```julia
module Play
using ..Game1Module
using ..Game2Module
export playgame
function playgame(x)
    if not(isfinished(x))
        nextmove(x)
    end
end
end

module Game1Module
export isfinished
struct Game1 end
isfinished(x::Game1) = true
nextmove(x::Game1) = printnl("Making a move in game 1")
end

module Game2Module
export isfinished
struct Game2 end
isfinished(x::Game2) = false
nextmove(x::Game2) = printnl("Making a move in game 2")
end

module Test
using Main.Play
using Main.Game1Module: Game1
game1 = Game1()
playgame(game1)
end

```

which is to have a few different games each of which implements the same set of functions which will allow me to know if the game has finished, what are the possible next moves etc, whose move it is now etc. And then I wanted to have an small engine which will use those functions to play the actual game, irrespective of whether it’s a Game1 or Game2. Hope it makes sense.

And the error is:

```julia
ERROR: UndefVarError: isfinished not defined
Stacktrace:
 [1] playgame(::Main.Game1Module.Game1) at ./REPL[166]:6
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [October 28, 2018, 12:58am UTC](https://discourse.julialang.org/t/julia-modules/16878/4 "2018-10-28T00:58:46Z")

</div>

I got your code to work by defining a “base” copy of `isfinished` and `nextmove` that all the other modules then overload.

EDIT: Just to clarify, the rule in Julia is that defining a name like `isfinished` at global scope in two different modules creates two different functions, not two methods of the same function, and hence the rules of multiple dispatch do not distinguish/relate them. In order to take advantage of multiple dispatch for a name spanning several modules, it is necessary for one module to own a master copy and for the others to `import` it before extending it.

By the way, instead of qualifying with `Main`, it is better to use `..` (see below) to improve portability. Also `printnl` should be `println` and `not` should be `!`.

```julia
module GameBase
isfinished() = nothing
nextmove() = nothing
export isfinished
end

module Game1Module
struct Game1 end
import ..GameBase.isfinished
import ..GameBase.nextmove
isfinished(x::Game1) = true
nextmove(x::Game1) = println("Making a move in game 1")
end

module Game2Module
struct Game2 end
import ..GameBase.isfinished
import ..GameBase.nextmove
isfinished(x::Game2) = false
nextmove(x::Game2) = println("Making a move in game 2")
end

module Play
import ..GameBase.isfinished
import ..GameBase.nextmove
using ..Game1Module
using ..Game2Module
export playgame
function playgame(x)
    if !(isfinished(x))
        nextmove(x)
    end
end
end

module Test
using ..Play
using ..Game2Module: Game2
game1 = Game2()
playgame(game1)
end

```
