# Programmatic retrieval of const documentation

**URL:** <https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308>\
**Category:** General Usage\
**Tags:** documentation\
**Created:** [January 13, 2020, 7:00pm UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308 "2020-01-13T19:00:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 13, 2020, 7:00pm UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308/1 "2020-01-13T19:00:05Z")

</div>

How do I programmatically get the documentation for a documented constant?

```julia

module MyMod

"""
This is MyAlias
""" 
const MyAlias = Val{:Alias}

end

using .MyMod

Docs.doc(MyMod.MyAlias)
....

```

“…” just gives the documentation for `Val`.

UPDATE:

I have a workaround for my particular situation but it doesn’t answer the more general question. I’m creating a constant in a macro so it looks a bit like this (note this is just for proof of concept and I didn’t really do any macro hygiene checking).

```julia
macro mymacro(const_name)
    const_name_symbol = QuoteNode(const_name)
    MyConst = :(const $const_name = Val{$const_name}())
    quote
        Base.@ __doc__ ($MyConst)

        function my_get_doc(::Type{Val{$sym_name}})
            return Base.Docs.doc(Base.Docs.Binding($(@ __MODULE__ ()), $(const_name_symbol)))
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 13, 2020, 9:24pm UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308/2 "2020-01-13T21:24:08Z")

</div>

Well, I got as far as using `Base.Docs.doc(Base.Docs.Binding(Main, :MyAlias))`. However, I can’t figure out how to make a function that figures out the module and name without using a macro.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [January 13, 2020, 11:53pm UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308/3 "2020-01-13T23:53:22Z")

</div>

```julia
@doc MyAlias

```

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 13, 2020, 11:57pm UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308/4 "2020-01-13T23:57:17Z")

</div>

That only works if I use a macro. If I use a function it no longer works.

```julia
julia> my_get_doc(x) = @doc x
my_get_doc (generic function with 1 method)

julia> my_get_doc(MyMod.MyAlias)
  No documentation found.

  Binding x does not exist.

```

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [January 14, 2020, 12:01am UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308/5 "2020-01-14T00:01:01Z")

</div>

Oh I see. I don’t think it’s possible, because docstrings are attached to symbols, not to values.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 14, 2020, 12:04am UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308/6 "2020-01-14T00:04:58Z")

</div>

> [@bramtayl](#):
>
> I don’t think it’s possible, because docstrings are attached to symbols, not to values.

It’s just a little odd because `Docs.doc` works on functions without using a macro to turn it into a symbol.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [January 14, 2020, 12:31am UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308/7 "2020-01-14T00:31:07Z")

</div>

Hmm interesting. After poking around in the internals, it looks like `Base.doc` is a lot smarter than just a symbol lookup, but clearly its not foolproof.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 14, 2020, 1:13am UTC](https://discourse.julialang.org/t/programmatic-retrieval-of-const-documentation/33308/8 "2020-01-14T01:13:13Z")

</div>

I briefly tried to look into it and I think the problem is that `Base.doc` looks at the type but `@doc` receives the exact expression and turns it into a symbol. If there were a way to find the name of the exact instance of the variable being passed then I think it would be possible.
