more enum and pragma troubles

JS js.mdnq at gmail.com
Sun Aug 18 22:31:55 PDT 2013


On Monday, 19 August 2013 at 04:37:58 UTC, captaindet wrote:
> On 2013-08-17 21:54, JS wrote:
>> On Sunday, 18 August 2013 at 00:17:22 UTC, captaindet wrote:
>>> On 2013-08-17 14:36, Jesse Phillips wrote:
>>>> Third you've declared a variable, bar, which will store your
>>>> enumerated value, 4. Variables are not compile time, even if 
>>>> the
>>>> value stored came from a compile time known value.
>>>
>>> yep, it completely escaped me that these are 'normal' 
>>> variables.
>>> and i have realized now that i can make them known at compile 
>>> time
>>> the same way as is done for other 'normal' variables, by 
>>> declaring
>>> them const ;)
>>>
>>
>> But if they are const then what good does that do you? Just 
>> use an
>> alias or enum in the first place?
>
> to be honest, i have not found a use case ;)
>
> highest priority was that i understood what was going on here...
>
> how did i get there? i was looking for an easy way to get 
> pragma(msg,...) to print the given name for an enumerated 
> value. in this example test2 or test4 instead of 2 and 4 - just 
> like write is doing it at runtime. so i was playing around with 
> the different variants of enum to see if i could get it working 
> somehow.. and got sidetracked....
>
> in the end i stuck to manifest constant enums storing the 
> enumerated values and found that
>
> pragma( msg, to!string(foo) );
>
> is doing the trick.
>
>
> /det

I had the same issues, it is not an issue with logic but with the 
weird way dmd works(pragma is handled very early on and before 
ctfe's are completely parsed).

Remember you can use ctfe's to format strings any way you want 
and even do logic and other stuff but you can't use pragma's 
inside the ctfe on ctfe variables(because as far as pragma is 
concerned, they are run time at that point).



module main;

import std.stdio, std.conv;

template foo(alias T)
{
     string foo(string s)
     {
		string x = to!string(T) ~ s ~ "yyyy";
		//pragma(msg, x); // pragma see's x as a run time variable(or 
rather pragma is executed before x is truly defined)
		return x;
     }
}
int main(string[] argv)
{

	pragma(msg, foo!("asf")("xxx")); // note, it prints what the 
commented pragma is suppose to print, but this one works!!! 
Because it is outside the ctfe and foo!(...) can be computed at 
compile time
	readln();
	return 0;
}

I really believe the issue is with the compiler and is a bug. 
Somehow x gets marked as run time, probably because ctfe's are 
suppose to work at run time too... even though in this instance 
it is being used at compile time.  (so it should work)


More information about the Digitalmars-d-learn mailing list