# If \`X\` is a module, why \`X.X\` also refers to \`X\`?

**URL:** <https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 6, 2019, 12:26am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892 "2019-05-06T00:26:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 6, 2019, 12:26am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/1 "2019-05-06T00:26:27Z")

</div>

What’s the point of this?

```julia
julia> module X end
Main.X

julia> X
Main.X

julia> X.X
Main.X

julia> X.X.X
Main.X

```

Because of this, I cannot define a function `X()` within `X`, for example.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [May 6, 2019, 4:59am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/2 "2019-05-06T04:59:09Z")

</div>

I’m guessing the reason for this is that you sometimes want to specify `X.foo()` to distinguish from `Y.foo()`, even within the module `X` (for clarity).

The naming convention is to capitalize the module, but not the function. So a module `Foo` might provide the function `foo`. Modules that are named after a type that they provide use the plural, so the module `Foos` might provide the type `Foo`.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 6, 2019, 5:20am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/3 "2019-05-06T05:20:24Z")

</div>

It’s possible that some use of it are not important anymore (e.g. it’s used sometimes in macros as incorrect hygiene and I’m pretty sure it was used in lowering). Do note that it is the reason you can use `Main` in the REPL though. (that one can be special cased if needed of course)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 6, 2019, 6:41am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/4 "2019-05-06T06:41:54Z")

</div>

I find this to be a very neat feature when I use explicit imports (in packages) and want the module name imported for using functions/variables that I would not otherwise specify with `using`, eg

```julia
using Foo: Foo, bar
bar() # available
Foo.baz() # qualified

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [May 6, 2019, 8:38am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/5 "2019-05-06T08:38:26Z")

</div>

Note that even if `X.X` didn’t refer to `X`, it would still be inconvenient to name the main function of module `X` as `X()`, because it could not be exported. (The module name and the function name would collide in the calling module.)

One could let modules have a “main function” that would be called using the module name. For example, like this:

```julia
(M::Module)(args...) = M. __main__ (args...) # Type piracy.

module X
   function __main__ (greeting)
       println(greeting, ", world")
   end
end

X("Hello")

```

(Of course, it’d be much cleaner if the function could be named `X` instead of ` __main__ `.)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 6, 2019, 8:53am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/6 "2019-05-06T08:53:36Z")

</div>

> [@Per](#):
>
> One could let modules have a “main function” that would be called using the module name.

I would find that pretty weird — the name `X` would refer to _two different objects in the same namespace_, depending on the context.

I am not sure what the use case is.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [May 6, 2019, 9:07am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/7 "2019-05-06T09:07:35Z")

</div>

> [@Tamas\_Papp](#):
>
> the name `X` would refer to _two different objects_

No. Look at my example code. The name `X` refers to the module. Only, modules would be callable, and calling a module would call it’s main function. To refer to the function you’d have to do `X. __main__ ` (or `X.X` if it were possible to name the function that.)

> [@Tamas\_Papp](#):
>
> I am not sure what the use case is

I guess one use would be “mini-modules” that only export one single function.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 6, 2019, 9:13am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/8 "2019-05-06T09:13:17Z")

</div>

Thanks for explaining. It is rather clever.

> [@Per](#):
>
> I guess one use would be “mini-modules” that only export one single function.

But, as you noted above, this is not encouraged naming style: modules are capitalized, functions are not. And when the exported callable is a constructor, the module name should be plural.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 6, 2019, 9:22am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/9 "2019-05-06T09:22:54Z")

</div>

> [@Tamas\_Papp](#):
>
> And when the exported callable is a constructor, the module name should be plural.

I think this recommendation came from the fact that you can’t define a type with the same name as the module it’s in.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 6, 2019, 11:33am UTC](https://discourse.julialang.org/t/if-x-is-a-module-why-x-x-also-refers-to-x/23892/10 "2019-05-06T11:33:03Z")

</div>

Precisely — and my understanding is that even with the workaround of @Per, this restriction would continue to hold.
