www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Add imports in examples?

Since 2.054 will allow function-local imports we could add imports in
examples. E.g. the upcoming std.benchmark module has this example:

--------------------
void foo()
{
    StopWatch sw;
    enum n = 100;
    TickDuration[n] times;
    TickDuration last = TickDuration.from!"seconds"(0);
    static void bar() {} // Add
    foreach(i; 0..n)
    {
       sw.start(); //start/resume mesuring.
       foreach(unused; 0..1_000_000)
           bar();
       sw.stop();  //stop/pause measuring.
       //Return value of peek() after having stopped are the always same.
       writeln((i + 1) * 1_000_000, " times done, lap time: ",
               sw.peek().msecs, "[ms]");
       times[i] = sw.peek() - last;
       last = sw.peek();
    }
    real sum = 0;
    // When you want to know the number of seconds,
    // you can use properties of TickDuration.
    // (seconds, mseconds, useconds, hnsecs)
    foreach(t; times)
       sum += t.hnsecs;
    writeln("Average time: ", sum/n, " hnsecs");
}
--------------------

But it's missing an import to std.conv and std.stdio. If there was an
import right there at the top the example is instantly compilable:

--------------------
void foo()
{
    import core.time;  // added
    import std.stdio;   // added
    StopWatch sw;
    ...
-------------------

There's also a missing function definition in that example but I'll
make a note for this on github.
Jul 01 2011