Re: Generate doc comment from template?

2020-04-07 Thread solo989
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)


Run

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)


Run


Generate doc comment from template?

2020-04-07 Thread spip
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)


Run

> that is instantiated like this:


foo(bar, "Doc comment about bar")


Run