# Extend julia module

**URL:** <https://discourse.julialang.org/t/extend-julia-module/123407>\
**Category:** General Usage\
**Tags:** question, module\
**Created:** [December 3, 2024, 11:59am UTC](https://discourse.julialang.org/t/extend-julia-module/123407 "2024-12-03T11:59:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [December 3, 2024, 11:59am UTC](https://discourse.julialang.org/t/extend-julia-module/123407/1 "2024-12-03T11:59:23Z")

</div>

Hi.  
I try to understand modules in julia.  
And cant figure out how to extend julia module.

Suppose i have a module:

```julia
module Test 
   t :: Float64 = 0
end

```

how to add new function or variable to the module from another file (or even file with declared module)?

New `module` declaration will override the previous.

Thanks

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [December 3, 2024, 12:18pm UTC](https://discourse.julialang.org/t/extend-julia-module/123407/2 "2024-12-03T12:18:09Z")

</div>

Let’s say you have a module Foo in file Foo.jl

```julia
module Foo
foo() = "hello!" 
end

```

And you’d like to add more to the module. The most common way is to `include` another file, for example bar.jl

```julia
module Foo
include("bar.jl")
foo() = "hello!" 
end

```

Then you can place code in bar.jl

```julia
bar(name) = "hello $name" 

```

---

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [December 3, 2024, 12:29pm UTC](https://discourse.julialang.org/t/extend-julia-module/123407/3 "2024-12-03T12:29:13Z")

</div>

Thanks for response!

So i should modify module directly to extend?  
No option like c++ namespaces. In REPL i will owerwrite module each time when i add something?

---

<div class="post-metadata">

**Author:** ![Pedro](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@Pedro](https://discourse.julialang.org/u/Pedro)\
**Post date:** [December 3, 2024, 12:35pm UTC](https://discourse.julialang.org/t/extend-julia-module/123407/4 "2024-12-03T12:35:27Z")

</div>

From what I know yes, you have to enclose everything between `module MODULE_NAME` and its corresponding `end`.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 3, 2024, 12:38pm UTC](https://discourse.julialang.org/t/extend-julia-module/123407/5 "2024-12-03T12:38:55Z")

</div>

It’s not clear what your use case is but one possible answer is that you can `eval` new things into an existing module.

```julia
julia> module Test
       f() = 1
       end
Main.Test

julia> @eval Test begin
       g() = 2
       end
g (generic function with 1 method)

julia> Test.f()
1

julia> Test.g()
2

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [December 3, 2024, 12:55pm UTC](https://discourse.julialang.org/t/extend-julia-module/123407/6 "2024-12-03T12:55:25Z")

</div>

Another way to extend a module from outside is multiple dispatch, that is, adding new methods for functions that are defined inside that module

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [December 3, 2024, 12:56pm UTC](https://discourse.julialang.org/t/extend-julia-module/123407/7 "2024-12-03T12:56:33Z")

</div>

You cannot introduce new values and functions to a module’s namespace externally without an `@eval` solution like @GunnarFarneback proposed, which sidesteps the issue because it evaluates the code within the module `Test`.

There is a standard way to extend a module in a somewhat restricted way: add methods to an existing function (what @gdalle mentioned).

```julia
julia> module A
       greeting() = "hello"
       end
Main.A

julia> A.greeting()
"hello"

julia> A.greeting(name) = "hello $name" 

julia> A.greeting("Sergey")
"hello Sergey"

```

---

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [December 4, 2024, 10:02am UTC](https://discourse.julialang.org/t/extend-julia-module/123407/8 "2024-12-04T10:02:25Z")

</div>

Thank for examples!
