This is half question and half meta-meta-feature request.
Library writer may like to pepper source code with small but complete and compilable examples of use. Like this:
# module A
...
proc foo() = ...
EXAMPLE:
import A
foo()
EXAMPLE:
import A
foo()
bar()
This may be implementable based on simple unit test here http://forum.nim-lang.org/t/653 and compiles function.
EXAMPLE "my-example":
import A
foo()
# or
EXAMPLE my-example:
import A
foo()
called as:
# hypothetical invocation syntax
app.exe --run-example:my-example
To make it usable probably requires cmdline parsing library being present by default (using -d:some-define feels infeasible here).
I imagine something like:
# this example would be shown near docs for foo()
EXAMPLE "my-example1" (foo):
foo()
# this example would be presented TWICE, near docs for foo() and for bar() as well
EXAMPLE "my-example2" (foo, bar):
foo()
bar()
I had problem to understand the documentation for compiler doc option but it seems hardwired.
I am also not sure about the ability to specify zero or more valid code symbols and have them verified during compilation (the (foo, bar) part).
Frequent solution for examples is to have them as separate project(s) in separate directory somewhere. This destroys close association with source code and they are not referred by programmer documentation. My suggestion here would allow to keep them "alive".
when defined("run-examples"):
foo()
and then run them with nim c -r -d:run-examples foo.nim?
You can define different switchs:
when defined(example1): # Code for example 1
when defined(example2): # Code for example 2
when defined(examplen): # Code for example n