Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
allow `import` inside `block`: makes N runnableExamples run N x faster, minimizes scope pollution #9300
block: import os
It currently gives:
As we improve our docs with more runnableExamples, this problem is gonna become more and more pressing. I often run into timeout issues on travis already (#8707), and appveyor is still taking often > 8 hours to process a build (https://github.com/nim-lang/Nim/issues/8640)
changed the title
allow `import` inside block: makes N runnableExamples run N x faster, minimizes scope pollution
Oct 10, 2018
From my experience with that feature in scala, I think it is not a good feature. When you have this feature, people will optimize "scope pollution" as you said it. But there isn't a real benefit to it, it's just a waste of time. Initially I did optimize this "scope pollution" but after I started using intellij which handles global imports automatically and I never had problems with it, I could dismiss local imports easily and never look back.
my proposal addresses the speed problem of running
that's already supported, and that was (IIRC) the reason why
 eg of one type of problem that can happen if runnableExamples are run in 1 single file without this proposal:
proc foo1() runnableExamples: import bar foo1(barfoo) ... proc foo2() runnableExamples: # oups, forgot to `import bar` foo1(barfoo)
but if a user copy pastes runnableExamples from foo2 into an editor, it will fail because
With this proposal, this would all work (and have the Nx speed benefit mentioned)
here's some benchmark:
gtime -v nim doc lib/pure/strutils.nim
Command being timed: "./bin/nim doc lib/pure/strutils.nim"
so that's a huge time saving, and one that'll only grow over time if runnableExamples becomes more common (which I hope).
Note: the previous issues we had with running all runnableExamples in a single file (eg causing some conflicts eg symbol clashes with multiple imports) are avoided in that commit by 1 level of indirection, each runnableExample still only has its own imports and nothing more, eg:
so, thanks @Araq for the fix!