The D Programming Language
  • Template Recursion
template factorial(int n)
{
  const factorial = n * factorial!(n-1);
}

template factorial(int n : 1)
{
  const factorial = 1;
}

void test()
{
  writefln(factorial!(4));  // prints 24 
}
  • Variadic Templates
void print(A...)(A a)
{
    foreach(t; a)
	writefln(t);
}
  • Lazy Evaluation of Function Arguments
void log(lazy char[] dg)
{
    if (logging)
	fwritefln(logfile, dg());
}

void foo(int i)
{
    log("Entering foo() with i set to " ~ toString(i));
}

  • Mixins
template GenStruct(char[] Name, char[] M1)
{
    const char[] GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
}

mixin(GenStruct!("Foo", "bar"));   // struct Foo { int bar; }

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-1) was last changed on 06-8월-2009 15:36 by UnknownAuthor