www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - small template and mixin frustration...

reply "WhatMeWorry" <kheaser gmail.com> writes:
// First attempt

string myDebug(string someSymbol)
{

     string doubleQuotes = "\" = \", ";
     string s = "writeln(__traits(identifier, " ~  someSymbol ~ 
")," ~
                 doubleQuotes ~ someSymbol ~ ");" ;
     return s;
}


// 2nd attempt - more generic - compile time errors at point of 
call.

string myDebug2(T)(T someSymbol)
{
     string doubleQuotes = "\" = \", ";
     string s = "writeln(__traits(identifier, " ~  
to!string(someSymbol)
                 ~ ")," ~ doubleQuotes ~ to!string(someSymbol) ~ 
");" ;
     return s;
}

// I thought just straight text replacement like here, but this 
fails at
// compile in the code block itself (this is not a function, 
right?)

template myDebug3(alias someSymbol)
{
     string doubleQuotes = "\" = \", ";
     writeln(__traits(identifier, " ~  someSymbol ~ ")," ~  
doubleQuotes ~ someSymbol ~ ");" ;
}



Trying to create the following statement programatically:

writeln(__traits(identifier, someSymbol), " = ", someSymbol);


GLfloat[6] staticVerts = [0.0, 1.0,-1.0, -1.0, 1.0, -1.0];
int i = 7;

mixin (myDebug("staticVerts"));  // This one works with output 
following
mixin (myDebug("i"));            // This one works with output 
following

staticVerts = [0, 1, -1, -1, 1, -1]
i = 7

So I thought it would be cool to get rid of the requirement for 
the arguments to be strings. That would make it simpler and also 
I could verify the arguments themselves.

What amazed me about D, was that I thought a typo like mixin 
(myDebug("taticVert")) would result in a run-time error.
but the compiler actually catches it!

However, with that said, I've spent so many hours beating my head 
against the wall that it has now become a matter of pride.  So,

mixin (myDebug2(staticVerts));   // Fails with the following...
mixin (myDebug2(i));             // Fails with the following...

MeGlWindow.d(72): Error: variable staticVerts cannot be read at 
compile time
MeGlWindow.d(72):        called from here: myDebug2(staticVerts)
MeGlWindow.d(72): Error: argument to mixin must be a string, not 
(myDebug2(staticVerts)) of type string
MeGlWindow.d(73): Error: variable i cannot be read at compile time
MeGlWindow.d(73):        called from here: myDebug2(i)
MeGlWindow.d(73): Error: argument to mixin must be a string, not 
(myDebug2(i)) of type string

Is what I'm trying to do possible?  Any suggestions or tips?  
Thanks.
Jun 06 2015
parent reply "sigod" <sigod.mail gmail.com> writes:
On Saturday, 6 June 2015 at 19:42:41 UTC, WhatMeWorry wrote:
 Trying to create the following statement programatically:

 writeln(__traits(identifier, someSymbol), " = ", someSymbol);


 GLfloat[6] staticVerts = [0.0, 1.0,-1.0, -1.0, 1.0, -1.0];
 int i = 7;

 mixin (myDebug("staticVerts"));  // This one works with output 
 following
 mixin (myDebug("i"));            // This one works with output 
 following

 staticVerts = [0, 1, -1, -1, 1, -1]
 i = 7
Why are you trying to use `mixin` while you can just call function? E.g. ``` GLfloat[6] staticVerts = [0.0, 1.0,-1.0, -1.0, 1.0, -1.0]; int i = 7; log(staticVerts); log(i); ``` (`log` is just an example.)
Jun 06 2015
parent reply "WhatMeWorry" <kheaser gmail.com> writes:
The solution was so trivial that I'm sorry I even posted. Here's 
what worked for me:

void myDebug(alias symbol)()
{
     string equalSign = " = ";
     writeln(symbol.stringof ~ equalSign, symbol);
}

The calls:

myDebug!i();
myDebug!staticVerts;
myDebug!boat;
myDebug!str;

Produced:

i = 7
staticVerts = [0, 1, -1, -1, 1, -1]
boat = 3.333
str = Hey, this works


Thanks again to Ali's wonderful book. Or more specifically, 
section 78.3.4 alias template parameters, p. 517.


How does .stringof, differ from __traits(identifier... ?
Jun 06 2015
parent "sigod" <sigod.mail gmail.com> writes:
On Saturday, 6 June 2015 at 21:52:32 UTC, WhatMeWorry wrote:
 How does .stringof, differ from __traits(identifier... ?
http://dlang.org/property.html#stringof:
 Note: Using `.stringof` for code generation is not recommended, 
 as the internal representation of a type or expression can 
 change between different compiler versions.

 Instead you should prefer to use the `identifier` trait, or one 
 of the Phobos helper functions such as `fullyQualifiedName`.
Jun 06 2015