Lorem ipsum dolor sit amet
Please elaborate on your question, be more explicit about what do you want to do or know. Do you have an example of desired behavior? Help us help you.
I want to replace the docstring of a function.
Let’s say I define a function with documentation.
"documentation"
function test_function()
end
Now I want to replace the documentation.
@doc "replacement" test_function
But the doc string is only appended to, not replaced.
@doc test_function
Ok, I’ve figured out a bit more what’s going on here.
"documentation"
function test_function()
end
replaces the docstring for a particular method,
while
@doc "replacement" test_function
seems to replace the docstring for the function in general.
So I’ve update the title accordingly
I’m not sure I understood your question, but you’ve to keep in mind that you can replace the docstring only for a method that has the same signature. If you use ?functionname
you’ll see the docstrings for all methods, if you want to see the docstring of a specific method, you have to specify the signature ?foo(arguments...)
.
See the following examples:
julia> foo(x::AbstractFloat) = x
foo (generic function with 1 method)
julia> foo(x::Integer) = 2x
foo (generic function with 2 methods)
julia> foo(x) = 3x
foo (generic function with 3 methods)
julia> @doc "old generic docstring" foo
foo
julia> @doc "old docstring for floating argument" foo(::AbstractFloat)
foo
julia> @doc "old docstring for integer argument" foo(::Integer)
foo
help?> foo
search: floor pointer_from_objref OverflowError RoundFromZero FileMonitor functionloc StackOverflowError @functionloc Factorization
old generic docstring
old docstring for floating argument
old docstring for integer argument
help?> foo(3)
old docstring for integer argument
help?> foo(4.0)
old docstring for floating argument
julia> @doc "new generic docstring" foo
WARNING: replacing docs for 'foo :: Union{}' in module 'Main'.
foo
julia> @doc "new docstring for floating argument" foo(::AbstractFloat)
WARNING: replacing docs for 'foo :: Tuple{AbstractFloat}' in module 'Main'.
foo
julia> @doc "new docstring for integer argument" foo(::Integer)
WARNING: replacing docs for 'foo :: Tuple{Integer}' in module 'Main'.
foo
help?> foo
search: floor pointer_from_objref OverflowError RoundFromZero FileMonitor functionloc StackOverflowError @functionloc Factorization
new generic docstring
new docstring for floating argument
new docstring for integer argument
help?> foo(5)
new docstring for integer argument
help?> foo(7.4)
new docstring for floating argument
Ok, that’s pretty helpful to know. But if I wanted to create
remove_docstrings_from(f::Function)
it would quite difficult, then? I’d have to figure out all method signatures and individually remove each docstring?
It would be helpful to know more about the problem you want to solve. Julia is a dynamic language, in the sense that you can manipulate the image, but people usually just add and overwrite docstrings, removing them is a very rare use case. (like they add variables and methods, removing either is not possible).
However, if you really insist, I guess you could look at the source of Base.Docs.doc!
and figure out a way to do it.
I think the basic question here is “why would you want to do that”? This hasn’t been answered yet.
If you use a method like delete_method
(in revise), you are still left with a docstring attached to the floating function.
You have to purge that to not get a warning in the console.
edit:
a use case would be:
loading every file in a directory
// note: you can not validate a function has all dependencies when you include it. (i mean you could, but who has time for that)
therefore, you just keep looping over all the files until everything gets loaded and you have no errors.
delete_method purges botched functions and then you have to remove the docs. hence the question
the code I used to resolve this can be found here:
For completeness, five years on, here’s how to clear a module’s docstrings for a particular binding:
function cleardocs!(mod::Module, name::Symbol)
docs = Docs.meta(mod)
docs[Docs.Binding(mod, name)] = Docs.MultiDoc() # empty multidoc
end
Docstrings for a module are stored in a dictionary accessed with Doc.meta(mod)
. As far as I know, it seems ok to modify this directly.
P.S., to answer the “why” question — I needed this because sometimes Revise.jl leads to doc double-ups until you restart the session, which is annoying.