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

>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
>

OK, this does pretty much what I wanted for this case, so this is good.
I wish I could say I understand it.  Is there somewhere, on the site or
elsewhere, that gives a good explanation of aliases outside of template
use?  Even in templates they're kind of tricky -- or unfamiliar, at
least.

>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.
>

At least once I've found myself writing macros that pieced together long
variable names that fell into a pattern.  I can't say with certainty
that doing this was really the best way to go about things, but I do
know that I had no choice in some of the variable names, as they were
part of a programming interface I was programming against.

So anyway, the ## allows you to do something like

#define callFooFuncion(prefix, varname, function) \
        prefix ## varname = somethingReallyHuge->member->submember->prefix ## 
function()

such that a call to

        callFooFunction(the, rabbits, reproduce)

would get expanded to

        therabbits = somethingReallyHuge->member->submember->thereproduce()

i.e., concatenating actual code, rather then the language's concept of
strings.

-Bob

>~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....
>>
>

-- 
Cheers,
Bob Schmertz

Reply via email to