Docstrings in macros

question

#1

What’s the right way of defining docstrings in macros? Should I just use @doc? MWE:

macro define_singleton(docstring, typename, constant = Symbol(uppercase(string(typename))))
    quote
        @doc $docstring immutable $typename end
        @doc $docstring const $(esc(constant)) = $typename()
    end
end

When I remove @doc, it does not even parse.


#2

Probably:

http://docs.julialang.org/en/stable/manual/documentation/#macro-generated-code

Or does that not work for your case?


#3

I was not aware of this, thanks! The final macro is

using MacroTools

"""
Define a singleton type with the given name and supertype (specified
as `name <: supertype`), and a constant which defaults to the name in
uppercase.
"""
macro define_singleton(name_and_supertype, constant = nothing)
    @capture name_and_supertype name_ <: supertype_
    if constant == nothing
        constant = esc(Symbol(uppercase(string(name))))
    end
    quote
        Core.@__doc__ immutable $name <: $supertype end
        Core.@__doc__ const $(constant) = $name()
    end
end