Is it possible to generate doc comments from templates? I have something like:
template foo(fn: untyped, doc: string) =
template fn*(a: Foo) =
"## " & doc <======== I would like to inject doc comment here
callfunc(fn, a)
that is instantiated like this:
foo(bar, "Doc comment about bar")
You could do
import macros
macro setDocComment(a : static[string]) : untyped =
result = nnkCommentStmt.newTree()
result.strVal = a
template foo(fn: untyped, doc: static[string]) =
proc fn*(a: Foo) =
setDocComment(doc)
callfunc(fn, a)
though it only immediately calls the macro setDoc if fn is a non generic proc
For a template or generic proc you could instead do
import macros
macro foo(fn: untyped, doc: static[string]) =
let
docString = nnkCommentStmt.newTree()
docString.strVal = doc
result = quote do:
proc `fn`*(a: Foo) =
`docString`
callfunc(`fn`, a)