This isn't really the purpose of templates in D...

However, it can be done.  The following program:

import tango.io.Stdout;

void showValue(alias D)() {
       Stdout(D.stringof)(" = ")(D).newline;
}

void main() {
       int x = 5;
       double d = 5.9;

       showValue!(x);
       showValue!(d);
}

prints:
[EMAIL PROTECTED] ~ $ ./test
x = 5
d = 5.90

I'm not familiar with C's ##, but D has ~ as concat, and it (along with
everything else) is evaluated at compile time whenever (the compiler detects
it to be) possible.

~John

On 5/7/07, Bob Schmertz <[EMAIL PROTECTED]> wrote:

John Demme incurred the wrath of Bob on May 4, by saying

>We didn't do over D's templates at the meeting tonight, so as promised
here
>is the article on them:
>
>http://digitalmars.com/d/templates-revisited.html
>

So maybe I've got the wrong idea about metaprogramming, but let me give
it a shot...

One of my favorite things about the C preprocessor (I know, we're all
supposed to hate the C preprocessor -- it's either too limiting or too
powerful, depending on where you're coming from) is the stringification
operator.  I'll often define a macro like

#define show_value(x) printf("value of " #x " is %d\n", x)

so that I can get the variable name into a string, and call it like
this for debugging:

    int x = 5;
    int y = 6;
    int z = 3;
    show_value(x);
    show_value(y);
    show_value(z);

Does D provide any way to convert a variable name to a string?

Going further, does it provide a way to do something comparable to the
concat operator for code (##) in CPP?

--
Cheers,
Bob Schmertz
After a long, long hiatus from the UMLUG list....

Reply via email to