# Module with one function of same name as the module?

**URL:** https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305
**Category:** General Usage
**Tags:** pkg, module
**Created:** [February 28, 2019, 2:33pm UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305 "2019-02-28T14:33:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mvdh7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvdh7/32/7271_2.png) [@mvdh7](https://discourse.julialang.org/u/mvdh7)
#### Post date: [February 28, 2019, 2:33pm UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/1 "2019-02-28T14:33:24Z")

</div>

I have a module that contains precisely one exported function. Is it possible to give the module and the function the same name and use them with the syntax below?

e.g. for the module:

```julia
module myfunc

export myfunc

function myfunc(args)
    return mystuff
end

end

```

I want to import and use with something like:

```julia
using myfunc
mystuff = myfunc(args)

```

Is there a way to do this (or similar), or do I just need to come up with a different name for the module than the function?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 28, 2019, 2:41pm UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/2 "2019-02-28T14:41:44Z")

</div>

It is a (quite strong) convention in Julia to write modules in CamelCase, so it would be called `MyFunc`. Is there a reason it is getting wrapped in a module?

---

<div class="post-metadata">

### Author: ![mvdh7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvdh7/32/7271_2.png) [@mvdh7](https://discourse.julialang.org/u/mvdh7)
#### Post date: [February 28, 2019, 3:02pm UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/3 "2019-02-28T15:02:08Z")

</div>

Thanks! The module wrapping is mostly because my impression is that this is the way to deploy the code for other people to use? There are also some non-exported functions within the module that are used by the main function.

---

<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: [March 1, 2019, 1:07am UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/4 "2019-03-01T01:07:57Z")

</div>

In that case a module seems like a great idea, but I’m afraid that you can’t have your function and module share the same name.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [March 1, 2019, 11:47am UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/5 "2019-03-01T11:47:53Z")

</div>

```julia
julia> module a
       _a() = println("Hello there!")
       function Base.getproperty(m::Module, x::Symbol)
           if m === a && x === :a
               return getfield(m, :_a)
           else
               return getfield(m, x)
           end
       end
       end
Main.a

julia> a.a()
Hello there!

```

---

<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: [March 1, 2019, 12:19pm UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/6 "2019-03-01T12:19:18Z")

</div>

Definitively don’t actually do this, though, since it modifies the way module lookup happens for _every_ module, not just yours.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 1, 2019, 12:44pm UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/7 "2019-03-01T12:44:00Z")

</div>

> [@rdeits](#):
>
> since it modifies the way module lookup happens for _every_ module, not just yours.

Agreed on definitely not using, although it should technically be safe due to the checks. Constant propagation will likely also make it free.

---

<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: [March 1, 2019, 12:58pm UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/8 "2019-03-01T12:58:17Z")

</div>

Agreed. Like all type piracy, it’s probably fine as long as only one person in the entire universe does it 😉

---

<div class="post-metadata">

### Author: ![mvdh7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvdh7/32/7271_2.png) [@mvdh7](https://discourse.julialang.org/u/mvdh7)
#### Post date: [March 1, 2019, 1:16pm UTC](https://discourse.julialang.org/t/module-with-one-function-of-same-name-as-the-module/21305/9 "2019-03-01T13:16:41Z")

</div>

Thank you!

The original desire to use only one name came because it’s a port of something from a different language, and I wanted to keep the API as consistent as possible. But in the end I’ve just come up with an acceptable alternative name for the container module.
