# Scope of module docstring

**URL:** https://discourse.julialang.org/t/scope-of-module-docstring/114085
**Category:** General Usage
**Tags:** question, documentation
**Created:** [May 10, 2024, 8:26am UTC](https://discourse.julialang.org/t/scope-of-module-docstring/114085 "2024-05-10T08:26:58Z")
**Posts on this page:** 6
**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: [May 10, 2024, 8:26am UTC](https://discourse.julialang.org/t/scope-of-module-docstring/114085/1 "2024-05-10T08:26:58Z")

</div>

Why does the following work?

```julia
"""
$(EXPORTS)
"""
module Foo
using DocStringExtensions
export bar
bar() = 1
end

```

Specifically, is the module docstring somehow scoped within the module, so that `EXPORTS` works?

I suspect that this is the case, however I cannot find it in the Julia docs.

[(see a more extensive example)](https://github.com/JuliaDocs/DocStringExtensions.jl/blob/ec66ad4a472241c7a7ae0686247fe578c5e50210/docs/Showcase/src/Showcase.jl#L4).

---

<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: [May 10, 2024, 8:51am UTC](https://discourse.julialang.org/t/scope-of-module-docstring/114085/2 "2024-05-10T08:51:30Z")

</div>

The docstring is attached from the inside of the module expression:

```julia
julia> Meta.@lower begin
       """
       $(EXPORTS)
       """
       module Foo
       using DocStringExtensions
       export bar
       bar() = 1
       end
       end
:($(Expr(:thunk, CodeInfo(
    @ REPL[2]:2 within `top-level scope`
1 ─ %1 = $(Expr(:toplevel, :(module Foo
  #= REPL[2]:5 =#
  #= REPL[2]:6 =#
  using DocStringExtensions
  #= REPL[2]:7 =#
  export bar
  #= REPL[2]:8 =#
  bar() = begin
          #= REPL[2]:8 =#
          1
      end
  (Base.Docs.doc!)(Foo, (Base.Docs.Binding)((var"@ __MODULE__")(), :Foo), (Base.Docs.docstr)((Core.svec)(EXPORTS, "\n"), (Dict{Symbol, Any})(:path => "REPL[2]", :linenumber => 2, (Pair)(:module, Foo))))
  end)))
└── return %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: [May 10, 2024, 8:55am UTC](https://discourse.julialang.org/t/scope-of-module-docstring/114085/3 "2024-05-10T08:55:00Z")

</div>

Thanks. Do you know if this is documented (ie something that users can rely on), or an accidental implementation feature? I could not find it in the docs.

---

<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: [May 10, 2024, 8:59am UTC](https://discourse.julialang.org/t/scope-of-module-docstring/114085/4 "2024-05-10T08:59:58Z")

</div>

I don’t think so. I wasn’t aware of this behavior until you posted.

---

<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: [May 10, 2024, 11:40am UTC](https://discourse.julialang.org/t/scope-of-module-docstring/114085/5 "2024-05-10T11:40:05Z")

</div>

It’s in [the docs](https://docs.julialang.org/en/v1/manual/documentation/#Modules) by implication:

```julia
"..."
module M end

module M

"..."
M

end

```

> Adds docstring `"..."` to the `Module` `M`. Adding the docstring above the `Module` is the preferred syntax, however both are equivalent.

The docstring definitely belongs to the scope of the documented module in the 2nd example, and that must also be the case in the 1st example for equivalence. None of the sections for other scope-introducing blocks share this pattern.

A minimal base Julia-only example:

```julia
julia> module A
         x = "a"
         "$x"
         module B
           x = "b"
         end
       end
Main.A

julia> @doc A.B
  b

```

---

<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 10, 2024, 12:13pm UTC](https://discourse.julialang.org/t/scope-of-module-docstring/114085/6 "2024-05-10T12:13:54Z")

</div>

I think it is better to document this explicitly, so I made a tiny PR.

> <https://github.com/JuliaLang/julia/pull/54429>
>
> Add a minor note to the documentation about the scope module docstrings are eval…uated in, with an example and a simple test.
> 
> Cf \[discussion\](https://discourse.julialang.org/t/scope-of-module-docstring/114085/)
