# Implicitly loaded modules in the future?

**URL:** <https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646>\
**Category:** Internals & Design\
**Tags:** question, module, code-organization\
**Created:** [June 9, 2021, 8:58pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646 "2021-06-09T20:58:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![panos.asproulis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panos.asproulis/32/23368_2.png) [@panos.asproulis](https://discourse.julialang.org/u/panos.asproulis)\
**Post date:** [June 9, 2021, 8:58pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/1 "2021-06-09T20:58:00Z")

</div>

I find the modules implementation in Julia the most annoying “feature” of the language.

For large projects it makes sense that developers would want to organise their code in separate modules (not packages), each one in a separate directory with a number of files. But, in Julia the executed code should be formed so that it includes each module once and one after the other in the proper order of usage. This is like manually joining together all the files forming the code in the right order and therefore the concept of modules loses its meaning. Are there any plans for the future to build a proper module system like that present in other languages e.g. Python or Java?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 9, 2021, 9:00pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/2 "2021-06-09T21:00:13Z")

</div>

The order of modules don’t matter if they are independent. If they are very interdependent, then why are they modules? If they are interdependent, don’t you need to specify the direction of the dependencies?

---

<div class="post-metadata">

**Author:** ![panos.asproulis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panos.asproulis/32/23368_2.png) [@panos.asproulis](https://discourse.julialang.org/u/panos.asproulis)\
**Post date:** [June 9, 2021, 9:08pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/3 "2021-06-09T21:08:24Z")

</div>

They can have dependencies. E.g. I may have a Mesh module which does some geometry work and then a Flow module which uses the mesh for solving the flow equations using various methods stored in separate modules which may need e.g, a Thermodynamics module and a Flow Properties module which needs to compute gradients and therefore it also needs the Mesh module etc.

In a large code there are all kinds of possible dependencies that cannot be done in a way that somehow the modules are loaded one after the other in some particular sequence that includes them once and yet the result actually works correctly.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 9, 2021, 9:10pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/4 "2021-06-09T21:10:45Z")

</div>

I think this is the same issue discussed here

> [@\[ANN\] PatModules.jl: a better module system for Julia](https://discourse.julialang.org/t/ann-patmodules-jl-a-better-module-system-for-julia/52226/38):
>
> Okay, quite a lot to unpack here. Some quotes-with-answers deliberately out of chronological order for better presentation. I’ll start off by apologising if I’ve come across the wrong way. I certainly don’t mean to offend anyone. Clearly I have a controversial opinion – I am trying to express disagreement without derogation. I’ll restate that for emphasis: I absolutely don’t mean to cause offense. Thanks for the welcome! Okay, let’s get into the meat of this. I’m constructing a module/pac…

---

<div class="post-metadata">

**Author:** ![panos.asproulis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panos.asproulis/32/23368_2.png) [@panos.asproulis](https://discourse.julialang.org/u/panos.asproulis)\
**Post date:** [June 9, 2021, 9:15pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/5 "2021-06-09T21:15:59Z")

</div>

I am not surprised to see that other people are also frustrated by the modules in Julia. I guess that if the primary purpose of Julia was to be used for Jupyter notebooks and relatively small pieces of code which make heavy use of external packages then the module system is not important. But for a large scale scientific/engineering project things become very difficult with this implementation of modular programming.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 9, 2021, 9:19pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/6 "2021-06-09T21:19:48Z")

</div>

I wouldn’t say that many people are frustrated with modules. One thread doesn’t prove it.  
I have used multiple modules in many (most of) my packages, and never with any ill effects or frustration.

---

<div class="post-metadata">

**Author:** ![panos.asproulis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panos.asproulis/32/23368_2.png) [@panos.asproulis](https://discourse.julialang.org/u/panos.asproulis)\
**Post date:** [June 9, 2021, 9:21pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/7 "2021-06-09T21:21:24Z")

</div>

Let’s say then that “some” people find it hard to cope with modules!

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 9, 2021, 9:26pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/8 "2021-06-09T21:26:36Z")

</div>

Rereading that thread I don’t quite understand the situation.

Can you lay out a simple concrete scenario to discuss?

---

<div class="post-metadata">

**Author:** ![panos.asproulis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panos.asproulis/32/23368_2.png) [@panos.asproulis](https://discourse.julialang.org/u/panos.asproulis)\
**Post date:** [June 9, 2021, 9:39pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/9 "2021-06-09T21:39:53Z")

</div>

What I understand regarding the way Julia works is that it needs to have a single file with all the code inside and in the proper sequence (so that some piece of code does not e.g. uses types not defined yet but only later on) and then it executes it line after line until the end.

So, if you use multiple files you need to include them to some main file and run this file. If you have modules again you need to include their source code manually so that they are only included once and in the right order. As a result, modules are not giving you any value like in other programming languages because you need to manually take care of their code so that at the end you create this large file with all the code for Julia to execute. And let’s not get into the whole dot notation for Julia to locate the modules with the “using” statements!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 9, 2021, 9:44pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/10 "2021-06-09T21:44:54Z")

</div>

It seems that the issues you raise were addressed in this thread

> [@Julia Modules](https://discourse.julialang.org/t/julia-modules/7028):
>
> The current situation with Julia modules is a nightmare with various keywords (include, using, import) and a lack of a simple and standard system for the compiler to find modules without playing around with source files. The same goes for exports. Rust has a nice and clean system of addressing these issues. Are there any plans for version 1.0 or beyond to provide a solution for this problem?

that you opened some years ago. There doesn’t seem to be any new complaints in this iteration.

Was the advice in that thread not helpful?

---

<div class="post-metadata">

**Author:** ![panos.asproulis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panos.asproulis/32/23368_2.png) [@panos.asproulis](https://discourse.julialang.org/u/panos.asproulis)\
**Post date:** [June 9, 2021, 9:48pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/11 "2021-06-09T21:48:06Z")

</div>

Apparently not! I’ve been waiting to see some improvements to the language ever since but modules remain the same.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 9, 2021, 9:55pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/12 "2021-06-09T21:55:06Z")

</div>

Okay, but the feedback in that thread was basically “don’t use `include` that way”. It seemed like your questions were addressed, but I don’t know how the advice fell short.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 9, 2021, 9:55pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/13 "2021-06-09T21:55:45Z")

</div>

If you are having trouble with modules, but other people manage apparently just fine, perhaps you are not using them in the right way?

---

<div class="post-metadata">

**Author:** ![panos.asproulis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panos.asproulis/32/23368_2.png) [@panos.asproulis](https://discourse.julialang.org/u/panos.asproulis)\
**Post date:** [June 9, 2021, 9:59pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/14 "2021-06-09T21:59:30Z")

</div>

If I convert my modules to packages then I would have no problems but is that the proper way to arrange a code when these packages are only meaningful as parts of a single and specific application?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 9, 2021, 10:06pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/15 "2021-06-09T22:06:30Z")

</div>

If you can convert the modules to individual packages, there should be no issues with interdependencies. In other words, such modules can be included and used in any order.

On the other hand, if the modules depend on functionality implemented in other modules, then either make your code one monolithic file without submodules (everybody lives in the same house - module), or separate into modules, but then the order in which the files are included to be compiled matters. Obviously, circular dependencies are not good. 😉

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [June 9, 2021, 10:21pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/16 "2021-06-09T22:21:47Z")

</div>

I should note that the PatModules.jl thread spawned [https://github.com/Roger-luo/FromFile.jl](https://github.com/Roger-luo/FromFile.jl) and [https://github.com/JuliaLang/julia/issues/39235](https://github.com/JuliaLang/julia/issues/39235). The former can be used now, and the latter looks like it has enough traction to happen at some point.

Edit: see also [https://github.com/JuliaLang/julia/issues/4600](https://github.com/JuliaLang/julia/issues/4600).

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 9, 2021, 10:40pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/17 "2021-06-09T22:40:37Z")

</div>

I don’t share this general frustration with modules, but one very specific thing that is probably related to what the OP describes as frustrating is this:

```julia
julia> module A
         f(x::M) = x.m
         struct M
           m
         end
       end
ERROR: UndefVarError: M not defined
Stacktrace:
 [1] top-level scope
   @ REPL[1]:2

```

Is it obvious why this does not work? (meaning, why the order matters there?).

Answering part of my question:

```julia
julia> module A
         f(x::Int) = x
         f(x::Int) = 2*x
       end
WARNING: replacing module A.
Main.A

julia> A.f(1)
2

```

But, then, couldn’t this simply throw an error? What are the situations in which good code requires that these two behaviours are as they are?

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [June 9, 2021, 10:45pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/18 "2021-06-09T22:45:27Z")

</div>

Isn’t your example intrinsic to how Julia handles forward references given that it’s not a statically compiled language?

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0-DEV.1640 (2020-12-02)
 _/ |\ __'_|_|_|\__'_| | Commit 9528ac2785* (188 days old master)
|__/ |

julia> f(x::T) = 1
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

```

---

<div class="post-metadata">

**Author:** ![panos.asproulis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panos.asproulis/32/23368_2.png) [@panos.asproulis](https://discourse.julialang.org/u/panos.asproulis)\
**Post date:** [June 9, 2021, 10:45pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/19 "2021-06-09T22:45:37Z")

</div>

This behaviour is the result of Julia processing the file line by line like executing one-line commands.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 9, 2021, 10:56pm UTC](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646/20 "2021-06-09T22:56:21Z")

</div>

Well, yes, but is anything in the module parsed before the “end” is returned? Is it possible to interact “inside” a module, such that that can’t be handled?

Having the things out of order, while sometimes practical, is certainly bad style. But having multiple conflicting definitions of the same method inside the module seems to be useless, and very bad style anyway. Are there “good” uses of that?

[Next page](https://discourse.julialang.org/t/implicitly-loaded-modules-in-the-future/62646.md?page=2)
