6

I am writing a macro, that takes a function name, and declares a couple of other version of the function. I want to give these variations the same doc string as the original method, with perhaps a few changes.

To do this, I need to retrieve the docstring for the orginal method.

So what I am looking for is a function:

get_docstring(functionname::Symbol, argtypes)::String

So that I could do:

julia> s=get_docstring(:values,(Associative,)) and then s would be set to:

s="""
    values(a::Associative)
Return an iterator over all values in a collection.
`collect(values(d))` returns an array of values.
```jldoctest
julia> a = Dict('a'=>2, 'b'=>3)
Dict{Char,Int64} with 2 entries:
  'b' => 3
  'a' => 2
julia> collect(values(a))
2-element Array{Int64,1}:
 3
 2
```
"""
Cœur
  • 37,241
  • 25
  • 195
  • 267
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137

1 Answers1

4

You can use the @doc macro, but instead of a string, it returns a markdown object:

julia> @doc "foo int" ->
       foo(x::Int) = x
foo

julia> @doc "foo float" ->
       foo(x::Float64) = x
foo

julia> @doc "foo sym" ->
       foo(x::Symbol) = x
foo

julia> @doc "foo x, y" ->
       function foo(x, y) x, y end
foo

julia> @doc foo
  foo int

  foo float

  foo sym

  foo x, y

julia> typeof(ans)
Base.Markdown.MD

julia> @doc foo(::Int)
  foo int

julia> @doc foo(::Float64)
  foo float

julia> @doc foo(::Symbol)
  foo sym

julia> md = @doc foo(::Any, ::Any)
  foo x, y

julia> md.content
1-element Array{Any,1}:
 foo x, y

julia> md.content[1]
  foo x, y

julia> md.content[1].content
1-element Array{Any,1}:
 Base.Markdown.Paragraph(Any["foo x, y"])

julia> md.meta
Dict{Any,Any} with 3 entries:
  :typesig => Tuple{Any,Any}
  :results => Base.Docs.DocStr[Base.Docs.DocStr(svec("foo x, y"),foo x, y…
  :binding => foo

julia>
HarmonicaMuse
  • 7,633
  • 37
  • 52