IIRC `@doc` works in global scope for retrieving documents. What you are seeing is just the difference in globals and locals.
see `doc/genstdlib.jl` for an example of how to iterate over documents. On Sat, Oct 31, 2015 at 10:05 AM, Dan Spielman <[email protected]> wrote: > I would like to write code that prints out all the docstrings in a module. > For example, here is my simple module: > > "doc this module" > module TestModule > > "add a to b" > function fx(a,b) > a+b > end > > export fx > end > > using TestModule > > > > The following works, but is very fragile: > > fnm = 0 # otherwise, get error > nms = names(TestModule) > for nm in nms > fnm = eval(nm) > docnm = @doc(fnm) > println(stringmime("text/plain",docnm)) > end > > > It produces > > doc this module > > > add a to b > > > Without the "fnm = 0", I get the error: > > LoadError: "fnm" is not defined in module Main > `fnm` is local to the for loop unless defined in the global scope already. > > If I try to wrap this loop in a function, I can get multiple behaviors. > The following is the best case, and assumes that I already ran the > previous block of code: > > > function printDocs(mod) > nms = names(mod) > > for nm in nms > fnm = eval(nm) > docnm = @doc(fnm) > println(stringmime("text/plain",docnm)) > end > end > printDocs(TestModule) > > > produces > > > add a to b > > > add a to b > > > (it lost the module, and turned it into fx) You are very likely just seeing the left over global variable fnm from your previous run in the global scope. > > If I run the function without previously printing the documentation, I get > > LoadError: "fnm" is not defined in module Main > And this confirm my guess. > > So, I try > > fnm = 0 > printDocs(TestModule) > and you are seeing the global variable `fnm` > > Which gives me the lovely documentation for zero: > Isn't it lovely ;-p > > 0 (zero; BrE: `/ˈzɪərəʊ/` or AmE: `/ˈziːroʊ/`) is both a number and the > numerical digit used to represent that number in numerals. It fulfills a > central role in mathematics as the additive identity of the integers, real > numbers, and many other algebraic structures. As a digit, 0 is used as a > placeholder in place value systems. Names for the number 0 in English > include zero, nought or (US) naught (`/ˈnɔːt/`), nil, or — in contexts where > at least one adjacent digit distinguishes it from the letter "O" — oh or o > (`/ˈoʊ/`). Informal or slang terms for zero include zilch and zip. Ought and > aught (/ˈɔːt/), as well as cipher, have also been used historically. > > > > If anyone can explain this, it would be very helpful. > I am using Julia 0.4.0 on a Mac running OS 10.11 > > Thanks, > Dan >
