www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - pragma(msg,

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
code:

template tct(T1,T2){
     string tct = T1.stringof ~ " " ~ T2.stringof ~
         typeof(true?T1.init:T2.init).stringof;
}
pragma(msg, tct!(shared(const(int))*, const(int*)));

result:

tct


why?
Jan 29 2011
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Jan 30, 2011 at 00:26, Ellery Newcomer
<ellery-newcomer utulsa.edu> wrote:
 code:

 template tct(T1,T2){
 =C2=A0 =C2=A0string tct =3D T1.stringof ~ " " ~ T2.stringof ~
 =C2=A0 =C2=A0 =C2=A0 =C2=A0typeof(true?T1.init:T2.init).stringof;
 }
 pragma(msg, tct!(shared(const(int))*, const(int*)));

 result:

 tct


 why?
Because the inner string is not a CT constant. Put an 'enum' before. Philippe
Jan 30 2011
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 01/30/2011 07:40 AM, Philippe Sigaud wrote:
 On Sun, Jan 30, 2011 at 00:26, Ellery Newcomer
 <ellery-newcomer utulsa.edu>  wrote:
 code:

 template tct(T1,T2){
     string tct = T1.stringof ~ " " ~ T2.stringof ~
         typeof(true?T1.init:T2.init).stringof;
 }
 pragma(msg, tct!(shared(const(int))*, const(int*)));

 result:

 tct


 why?
Because the inner string is not a CT constant. Put an 'enum' before. Philippe
doh. when I did something like enum string s = tct!(int,int); it just took it and didn't complain that it didn't know the value at compile time.
Jan 30 2011
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Jan 30, 2011 at 19:15, Ellery Newcomer
<ellery-newcomer utulsa.edu> wrote:
 doh.

 when I did something like

 enum string s = tct!(int,int);

 it just took it and didn't complain that it didn't know the value at compile
 time.
I think in this case, your asking for an enum string, er, 'propagates' the compile-time-ness to tct!(int,int). Everything is calculated at CT, because it's doable. But when you used pragma(msg, someString), the compiler doesn't know someString can be entirely known at CT. So, there is a difference between pragma(msg, tct!(T,U)); and enum string s = tct!(T,U)); pragma(msg, s); But if you indicate to the compiler you want the result of tct to be a CT string, everything is resolved. So the easiest way is to put enum string tct = ... inside the template. I spent some time building a CT regex engine in D. Having the match/replace results shown with pragma instructions is fun, but I had horrible moments trying to understand the interplay between compile-time et runtime... Philippe
Jan 30 2011
parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 01/30/2011 03:47 PM, Philippe Sigaud wrote:
 On Sun, Jan 30, 2011 at 19:15, Ellery Newcomer
 <ellery-newcomer utulsa.edu>  wrote:
 doh.

 when I did something like

 enum string s = tct!(int,int);

 it just took it and didn't complain that it didn't know the value at compile
 time.
I think in this case, your asking for an enum string, er, 'propagates' the compile-time-ness to tct!(int,int). Everything is calculated at CT, because it's doable. But when you used pragma(msg, someString), the compiler doesn't know someString can be entirely known at CT.
actually, it didn't calculate it and if I passed it to a function call, then it would complain that it didn't know the value
Jan 30 2011