Bragging about Unix and D
Tom S
h3r3tic at remove.mat.uni.torun.pl
Wed Aug 23 18:55:34 PDT 2006
Georg Wrede wrote:
> What I tried to do was some kind of template magic (a la Don) that
> prints something like this at compile time, without even creating an
> executable.
>
> -- Sigh, can't win 'em all.
Not hard at all...
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) {
mixin .table!(i+1) table;
} else {
alias void table;
}
}
alias table!() draw;
--
Tomasz Stachowiak
More information about the Digitalmars-d
mailing list