= Draw Table at Compile Time Example = ''Part of'' TemplatesCategory == Description == Demonstrates how a template function can be used to draw a table at compile time. {{{ #!d const int numWidth = 6; const int width = 10; const int height = 10; template itoa(int i) { static if (i < 10) { const char[] itoa = "" ~ "0123456789"[i]; } else { const char[] itoa = itoa!(i/10) ~ "0123456789"[i%10]; } } template nspaces(int n) { static if (n > 0) { const char[] nspaces = " " ~ nspaces!(n - 1); } else { const char[] nspaces = ""; } } template itoa_pad(int i) { const char[] itoa_pad = itoa!(i) ~ nspaces!(numWidth - itoa!(i).length); } template row(int hpos, int i = 0) { static if (i+1 < width) { const char[] row = itoa_pad!(i*hpos) ~ row!(hpos, i+1); } else { const char[] row = itoa_pad!(i*hpos); } } template table(int i = 0) { pragma (msg, row!(i)); static if (i+1 < height) { alias .table!(i+1) table; } else { alias void table; } } alias table!() draw; }}} == Example Batch File == (Just compile it since the table is drawn at compile time and there is no main) {{{ @echo off dmd DrawTableAtCompileTimeExample.d -c pause }}} == Output == {{{ 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 2 4 6 8 10 12 14 16 18 0 3 6 9 12 15 18 21 24 27 0 4 8 12 16 20 24 28 32 36 0 5 10 15 20 25 30 35 40 45 0 6 12 18 24 30 36 42 48 54 0 7 14 21 28 35 42 49 56 63 0 8 16 24 32 40 48 56 64 72 0 9 18 27 36 45 54 63 72 81 }}} == Tested Version == Tested with Digital Mars D Compiler v0.165 running under Windows 2000. == Source == From [http://lists.puremagic.com/pipermail/digitalmars-d/2006-August/007773.html digitalmars-d/2006-August/007773.html] (with improvement from [http://lists.puremagic.com/pipermail/digitalmars-d/2006-August/007807.html digitalmars-d/2006-August/007807.html]).