# Using symbols from parent module without naming it

**URL:** <https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 20, 2023, 11:57am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833 "2023-07-20T11:57:22Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [July 20, 2023, 11:57am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/1 "2023-07-20T11:57:22Z")

</div>

Is it possible to import/using a symbol for a _parent_ module without naming it? Eg

```julia
module Foo
const c = 1
module Bar
using ..Foo:c # like this, but without referring to Foo
f() = c
end
end

```

---

<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:** [July 20, 2023, 12:18pm UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/2 "2023-07-20T12:18:59Z")

</div>

`const c = parentmodule(@ __MODULE__ ).c` maybe…

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 20, 2023, 1:48pm UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/3 "2023-07-20T13:48:48Z")

</div>

How about this: `@.. c` (or `@.. c d e` - if you need to import multiple symbols).

The macro definition here:

```julia
macro (..)(s...)
    Expr(:using,
        Expr(:(:), Expr(:., :., :., Symbol(nameof(parentmodule( __module__ )))),
            (Expr(:., (Symbol(x))) for x in s)...))
end

```

In your context:

```julia
module Foo
const c = 1
const d = 2
module Bar

macro (..)(s...)
    Expr(:using,
        Expr(:(:), Expr(:., :., :., Symbol(nameof(parentmodule( __module__ )))),
            (Expr(:., (Symbol(x))) for x in s)...))
end

# works with importing multiple symbols
@.. c d

fc() = c
fd() = d

@info "happy import: " fc() fd()

end
end

```

Running the program would produce:

```julia
┌ Info: happy import: 
│ fc() = 1
└ fd() = 2

```

Not sure if this is reliable enough for any context (limited testing) - I am sure it can be improved.

Later edit: the initial version only worked if the macro was defined inside the calling module. The current version of the macro works even if defined in module X and invoked from Y (it will still import symbols from the parent of Y).

LE2: renamed it for more convenience. You can invoke it by `@.. c d e f g`.

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [July 20, 2023, 1:54pm UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/4 "2023-07-20T13:54:54Z")

</div>

I was wondering the same just a few days ago. I don’t think you can. However, it _should_ be

```julia
using ..: c

```

if it were to be consistent. Likewise, `using ...:` should be importing from the grandparent.

---

<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:** [July 20, 2023, 2:19pm UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/5 "2023-07-20T14:19:19Z")

</div>

Yes, I am wondering if I should open an issue, as that would be the natural syntax for it. Could not find an existing one.

---

<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:** [July 21, 2023, 5:10am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/6 "2023-07-21T05:10:49Z")

</div>

Issue opened:

> <https://github.com/JuliaLang/julia/issues/50616>
>
> After \[this discussion\](https://discourse.julialang.org/t/using-symbols-from-par…ent-module-without-naming-it/101833), I am under the impression that there is no way to \`using\` (or \`import\`) a symbol from the parent module without naming the latter explicitly, as in
> 
> \`\`\`julia
> module Foo
> const c = 1
> module Bar
> using ..Foo:c # like this, but without referring to Foo
> f() = c
> end
> end
> \`\`\`
> 
> While one can always name the parent module, if submodules are grouped into files this requires keeping track of parent names, which in turn imposes a minor cost when reorganizing or renaming modules.
> 
> If feasible, a natural syntax would be
> \`\`\`julia
> using ..:c
> \`\`\`

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 21, 2023, 8:40am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/7 "2023-07-21T08:40:17Z")

</div>

Even if it was possible to do `using ..: c`, I would not recommend to do it when submodules are written in separate files, which seems to be the use case that @Tamas_Papp has in mind according to his comment in the github issue.

Yes, it would reduce the workload of refactoring code, as suggested. But on the other hand, it might make the code less understandable. Let’s say `Foo.jl` is:

```julia
module Foo
const c = 1
include("Bar.jl")
end

```

And `Bar.jl` is:

```julia
module Bar
using ..: c
f() = c
end

```

How do I know what `c` is and where does it come from, if I’m just reading `Bar.jl`? I should scan all the other files of the project until I find one with the code `include("Bar.jl")`, instead of going directly to `Foo.jl`, which would be the obvious choice with the explicit `using ..Foo:c`.

(Although admittedly, writing out the name of the module only helps if the names of the files are a good hint to look for the definitions of modules; otherwise the situation is not that different.)

---

<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:** [July 21, 2023, 9:08am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/8 "2023-07-21T09:08:15Z")

</div>

> [@heliosdrm](#):
>
> How do I know what `c` is and where does it come from, if I’m just reading `Bar.jl`?

I am not sure I understand your question, but the (proposed) syntax clearly specifies that it comes from the parent module.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 21, 2023, 9:25am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/9 "2023-07-21T09:25:30Z")

</div>

What I mean is that if the parent module is in another file, I’ll have to look for that file if I want to know more details about what `c` is, how it is defined, etc. And explicitly naming the parent module _may_ be helpful to do the search.

---

<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:** [July 21, 2023, 9:41am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/10 "2023-07-21T09:41:16Z")

</div>

But that applies to all code. If I see a symbol that is not defined in a file, I have to find it if I want to know all the details.

Or, ideally, I can look up the docs if it is documented and that may be sufficient. But the tooling for that transcends modules, so it is fine.

Sorry, I still don’t understand what your point is, in the sense of something special going on with importing symbols from the parent module, as opposed to just importing symbols from anywhere else.

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 21, 2023, 9:59am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/11 "2023-07-21T09:59:31Z")

</div>

I am not sure how much time will pass until the new syntax recommendation will be picked up (but since it is non-breaking and easy to implement, who knows).

Until then, besides the `@.. c d` approach [above](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/3) (which I prefer), I think another quick workaround can be something like this:

```julia
module Foo

const c = 1
const d = 2

module Bar

struct pm end
Base.propertynames(::Type{pm}) = names(parentmodule(@ __MODULE__ ))
Base.getproperty(::Type{pm}, name::Symbol) = getproperty(parentmodule(@ __MODULE__ ), name)

# Now, you can use `c` and `d` directly:
fc() = pm.c
fd() = pm.d

@info "happy: " fc() fd()

end
end

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 21, 2023, 10:07am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/12 "2023-07-21T10:07:31Z")

</div>

How far up the parentmodule chain would you like to go? I’d imagine something like `using ........: foo` to be quite confusing - can you see at a glance how many parentmodules that is?

As an aside - what’s the usecase where this is desirable to do? Or is that purely for the (unique) parent module?

---

<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:** [July 21, 2023, 10:17am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/13 "2023-07-21T10:17:54Z")

</div>

> [@Sukera](#):
>
> How far up the parentmodule chain would you like to go?

Since there is no limitation on the `using ...` syntax for _modules_, I see no reason to impose one here.

> [@Sukera](#):
>
> As an aside - what’s the usecase where this is desirable to do?

You need a symbol from the parent, which is uniquely identified by being the parent, so DRY.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 21, 2023, 10:28am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/14 "2023-07-21T10:28:23Z")

</div>

Hmm. I’m personally not a fan and I agree with the posters above - this feels _too_ DRY, to the point of obscuring the meaning/source of the used symbol for someone who isn’t intimately familiar with the codebase in question. “DRY” doesn’t mean “write as little human-legible code as possible”, it can certainly be taken too far.

Not to mention that these long chains of dots are incredibly hard to parse visually.

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 21, 2023, 10:53am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/15 "2023-07-21T10:53:01Z")

</div>

> [@Sukera](#):
>
> Not to mention that these long chains of dots are incredibly hard to parse visually.

We’ll need some rainbow effect - where each dot has a different color. 🙂 Or we can add inlay hints to the vs-code extension that will read _7 dots and counting_.

Joke aside, I am pretty comfortable with having a unique syntax to import stuff from the parent module (especially since that is uniquely identified in the submodule context - so no ambiguity there) - as long as it is properly documented and becomes part of the knowledge base. (`..:` is OK).

Also, if the tooling improves and newcomers can hover `..:` and have instant information about what is going on, then such additions might not feel like we are compromising in readability.

---

<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:** [July 21, 2023, 11:49am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/16 "2023-07-21T11:49:59Z")

</div>

> [@Sukera](#):
>
> these long chains of dots are incredibly hard to parse visually

Note that

1. this is orthogonal to the issue; we **already** have [the `.......` syntax](https://docs.julialang.org/en/v1/manual/modules/#Submodules-and-relative-paths), of arbitary length, just that it only works for modules, not all symbols,

2. it is in practice limited by how much people nest modules. arguably if someone nests \>5 layers of modules, they have much larger problems than having to write/read `.....`.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [August 14, 2023, 9:08pm UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/17 "2023-08-14T21:08:44Z")

</div>

An ugly solution in the vein of @kristoffer.carlsson’s solution, but using the standard `using` mechanism:

```julia
@eval using ..$(nameof(parentmodule(@ __MODULE__ ))): c

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 14, 2023, 10:10pm UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/18 "2023-08-14T22:10:13Z")

</div>

> [@Tamas\_Papp](#):
>
> You need a symbol from the parent, which is uniquely identified by being the parent, so DRY.

> [@heliosdrm](#):
>
> How do I know what `c` is and where does it come from, if I’m just reading `Bar.jl`? I should scan all the other files of the project until I find one with the code `include("Bar.jl")`, instead of going directly to `Foo.jl`, which would be the obvious choice with the explicit `using ..Foo:c`.

The point was for Bar.jl to be `include`-able in many parent modules with the condition that those modules have a `c`, which I assume would be documented with more details (like type) in Bar.jl, like an interface. It’s not unlike how `include`-able files containing methods can be included in different modules to be in separate functions; that file may also assume certain globals exist (and should be documented).

Thing is, I agree it’s unwise to make easier, and it’s not just because it’s harder to read. It’s already very unusual to make nearly identical methods in different functions in different modules, and when it does happen, it probably should be refactored. This would make nearly identical modules in different parent modules, which should still be refactored instead of introducing new syntax. Although this seems like a DRY (don’t repeat yourself) principle because it reduces copies in the source code, `include`-ing identical source code in different modules to make structures that behave slightly differently depending on the global variables around is absolutely not DRY because the running process is a system too, and it has multiple tweaked copies with identical names.

Even the source code is not DRY much. A perk of DRY is that you can make a change in one place instead of several. So let’s check that, what if you had to rename `c` to `d`? Sure you can do that in one place for `Bar` modules, but you still have to track down every parent module. What if you had to change the various `c` to one new type? You can’t even do that in Bar.jl, you must find every parent module.

---

<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:** [August 15, 2023, 8:25am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/19 "2023-08-15T08:25:21Z")

</div>

> [@Benny](#):
>
> It’s already very unusual to make nearly identical methods in different functions in different modules, and when it does happen, it probably should be refactored.

I am not sure where you got impression that this is what I am trying to do. I think you may be misunderstanding something in my question — please read the original post.

Julia already provides the `...` syntax to access parent modules in various ways, what I am asking about is simply a missing corner case. I don’t think it was omitted by design, it was simply overlooked and I hope it will be fixed as some point.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 15, 2023, 8:41am UTC](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833/20 "2023-08-15T08:41:16Z")

</div>

I supposed I might’ve misunderstood something from the thread. So you intend submodules in separate `include`d files but not to include them several times in different places?

> [@Tamas\_Papp](#):
>
> I don’t think it was omitted by design, it was simply overlooked

If it were intentional, it would have good reason. It is difficult to visually tell the difference between `..` and `...`, and a file doesn’t innately point out other relevant files, so the name makes it much easier to find parent modules. I see your point that renaming would need changes in several places, but it’s not worse than renaming other global variables, easy with a Find and Replace.

[Next page](https://discourse.julialang.org/t/using-symbols-from-parent-module-without-naming-it/101833.md?page=2)
