# Access docstring within a macro

**URL:** <https://discourse.julialang.org/t/access-docstring-within-a-macro/90362>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [November 16, 2022, 4:39pm UTC](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362 "2022-11-16T16:39:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [November 16, 2022, 4:39pm UTC](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362/1 "2022-11-16T16:39:27Z")

</div>

Hello

Is there any way to access the docstring accompanying a macro at call place? It would be something like this:

```julia
macro MyMacro(arg)
# Do something with my docs
# It does not mean that I want to append the outer doc to some inner object using Main.@ __doc__
end

"I want to access this docstring"
@MyMacro argument

```

I have seen this topic, which is closely related, but no response was given: [https://discourse.julialang.org/t/accessing-the-docstring-from-within-a-macro-doc-troubles](https://discourse.julialang.org/t/accessing-the-docstring-from-within-a-macro-doc-troubles)

Thanks!

---

<div class="post-metadata">

**Author:** ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)\
**Post date:** [December 19, 2022, 5:21pm UTC](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362/2 "2022-12-19T17:21:32Z")

</div>

Did you find an answer? I’d be interested in this too…

I tried to use a combination of `@ __doc__ ` and `@doc`, but that didn’t work, presumably due to scheduling issues. (The idea was to label a variable within the macro using `@ __doc__ `, so that the docstring is attached to this variable. Then, hypothetically, one could call`@doc` on the same variable to retrieve the docstring again. However, when doing so I always get “No documentation found” because the “binding `variable` does not exist”.)

---

<div class="post-metadata">

**Author:** ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)\
**Post date:** [December 19, 2022, 5:29pm UTC](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362/3 "2022-12-19T17:29:55Z")

</div>

@mike, you wrote most of the doc system, can you help us out?

(For more context, I’m working on [this issue](https://github.com/JuliaDynamics/Agents.jl/issues/715)).

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [December 19, 2022, 7:31pm UTC](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362/4 "2022-12-19T19:31:34Z")

</div>

Yes, I was able to advance a little bit further, though not much because my day job is taking all my time.

It gets super complicated, but the scheduling issue is solved calling Doc undocumented methods manually over the created object. The undocumented methods are a mess because they use as parameters undocumented features (IdDicts and more) and access module-level undocumented fields directly and such.

I’m on my phone so I can’t say much else, sorry

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [December 19, 2022, 8:57pm UTC](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362/5 "2022-12-19T20:57:37Z")

</div>

I’ve left a comment on how to go about adding basic support for it in the case of `@agent` in the linked issue. Hopefully that helps 🙂

---

<div class="post-metadata">

**Author:** ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)\
**Post date:** [December 19, 2022, 9:26pm UTC](https://discourse.julialang.org/t/access-docstring-within-a-macro/90362/6 "2022-12-19T21:26:24Z")

</div>

Fantastic, thanks so much for this quick help!

For posterity: In my case, the problem was that the macro I’m working on creates a struct that I wanted to be able to document. However, the struct has to be created inside a double quote, and therefore one can’t simply apply `@ __doc__ ` to it. However, one can subsequently simply apply `@ __doc__ ` to the symbol that names the new struct, thus obviating the need for any more fancy metaprorgamming.
