# Loop over function names to document them

**URL:** <https://discourse.julialang.org/t/loop-over-function-names-to-document-them/25328>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [June 15, 2019, 4:45pm UTC](https://discourse.julialang.org/t/loop-over-function-names-to-document-them/25328 "2019-06-15T16:45:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [June 15, 2019, 4:45pm UTC](https://discourse.julialang.org/t/loop-over-function-names-to-document-them/25328/1 "2019-06-15T16:45:54Z")

</div>

I have written a set of functions, say `foo!`, `bar!`, etc., to do some operations that modify their first argument. Now say I want to have their complements `foo`, `bar`… that operate on a default value of that first argument, e.g.

```julia
foo(a, args...) = foo!(defaultobject(), args...)

```

Instead of repeating that line with all the functions, if I’ve got all function names as symbols in the collection `fnames` I can just do:

```julia
for f in fnames
    f! = Symbol(f, :!)
    @eval $f(args...) = $f!(defaultobject(), args...)
end

```

But now I want `foo` and `foo!`, etc. to have a common docstring. I have tried to do this but it fails:

```julia
fdocs = Dict(
    :foo => "documentation of foo and foo!",
    :bar => "documentation of bar and bar!"
    # etc.
)

for (f, docstring) in fdocs
    f! = Symbol(f, :!)
    @eval $f(args...) = $f!(defaultobject(), args...)
    @doc docstring $f
    @doc docstring $f!
end

```

The error message that I get is:

```julia
ERROR: LoadError: LoadError: LoadError: cannot document the following expression:
$(Expr(:$, :f))

```

It works well if I write directly  
`@doc docstring :foo`  
and so on. What am I doing wrong in the loop?

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [June 16, 2019, 11:51pm UTC](https://discourse.julialang.org/t/loop-over-function-names-to-document-them/25328/2 "2019-06-16T23:51:32Z")

</div>

You also need to `@eval` the call to `@doc`.

Here is an MWE, based on your example:

```julia
fdocs = Dict(
    :foo => "returns 1",
    :bar => "returns 1 also"
    # etc.
)

for (f, docstring) in fdocs
    @eval $f() = 1
    @eval @doc $docstring $f
end

```

```julia
help?> foo
search: foo floor pointer_from_objref OverflowError RoundFromZero unsafe_copyto!

  returns 1

help?> bar
search: bar baremodule SubArray GlobalRef clipboard BitArray backtrace BitMatrix

  returns 1 also

```
